CGPT R Debugger
Use this skill when the user asks to debug, profile, stress test, or optimize R code. Keep the work empirical and reproducible: run the smallest useful test harness, capture the error or timing evidence, and report the exact failure mechanism before proposing changes.
This is not broad R tutoring. Focus on concrete runtime behavior, profiling evidence, edge cases, and safe fixes.
Guardrails
- Do not edit the user's R, QMD, data, or output files unless they explicitly ask for implementation after seeing the diagnosis.
- Prefer temporary scripts in the working directory or a project
diagnostics/ or quality_reports/ folder only when the user permits artifacts.
- Do not install packages without approval. If a profiling package is unavailable, fall back to base R tools.
- Preserve project conventions from
CLAUDE.md, MEMORY.md, _quarto.yml, renv files, and existing scripts.
- For analysis claims, distinguish observed runtime evidence from inferred causes.
Workflow
- Identify the target code path, expected output, inputs, side effects, and package/session assumptions.
- Reproduce the issue with the smallest runnable harness:
- source the function or script
- load required inputs
- set seeds and options when randomness or printing affects results
- capture
sessionInfo(), warnings, errors, and traceback
- Add edge-case tests based on the code:
- zero rows, one row, all
NA, mixed types, duplicate keys, missing columns, factor versus character, unusual names, date/time boundaries, and larger synthetic inputs
- Profile only after correctness is understood:
- use
system.time() for a baseline
- use
Rprof() and summaryRprof() for base-R profiling
- use
bench, microbenchmark, or profvis only when already available or approved
- Classify the issue:
- correctness bug
- dependency or environment mismatch
- data-shape assumption
- numerical or statistical edge case
- memory pressure
- vectorization or algorithmic bottleneck
- rendering or Quarto execution failure
- Report the diagnosis with evidence and the smallest safe fix.
R Debugging Patterns
Use base tools first:
tryCatch(
source("script.R"),
error = function(e) {
message("ERROR: ", conditionMessage(e))
traceback()
},
warning = function(w) {
message("WARNING: ", conditionMessage(w))
invokeRestart("muffleWarning")
}
)
sessionInfo()
For profiling:
baseline <- system.time(source("script.R"))
print(baseline)
Rprof("profile.out")
source("script.R")
Rprof(NULL)
print(summaryRprof("profile.out"))
For edge-case checks, build a small table of test inputs and record actual versus expected behavior. Do not treat passing one happy-path run as sufficient when the requested problem concerns fragility.
Performance Triage
Look specifically for:
- growing objects in loops with
rbind, cbind, or c(...)
- row-wise loops over data frames where vectorized or grouped operations are clearer
- repeated file I/O inside loops
- repeated joins, filters, or model fits that can be hoisted or cached
- implicit type conversions, factor level loss, and date parsing inside tight loops
- non-deterministic order from joins, grouping, or parallel execution
- memory-heavy copies of large data frames
When recommending a refactor, show the before/after pattern only for the relevant lines and include timing evidence when possible.
Report Format
Use this compact structure:
# R Debugging Report
## Summary
- Target:
- Status: reproduced / not reproduced / partially reproduced
- Main cause:
- Proposed next action:
## Evidence
| Check | Result | Notes |
|---|---|---|
## Edge Cases
| Case | Expected | Actual | Status |
|---|---|---|---|
## Profiling
| Path or function | Time or share | Evidence |
|---|---:|---|
## Recommended Fixes
| Priority | Location | Fix | Rationale |
|---|---|---|---|
If the user asks for implementation, apply only the approved or explicitly scoped edits and rerun the reproduction harness afterward.
1---2name: cgpt-r-debugger3description: Reproducible R debugging, profiling, and edge-case testing for scripts, functions, Quarto analysis chunks, and data pipelines; use when the user asks to diagnose incorrect R output, slow code, brittle behavior, vectorization options, memory use, or runtime failures.4---56# CGPT R Debugger78Use this skill when the user asks to debug, profile, stress test, or optimize R code. Keep the work empirical and reproducible: run the smallest useful test harness, capture the error or timing evidence, and report the exact failure mechanism before proposing changes.910This is not broad R tutoring. Focus on concrete runtime behavior, profiling evidence, edge cases, and safe fixes.1112## Guardrails1314- Do not edit the user's R, QMD, data, or output files unless they explicitly ask for implementation after seeing the diagnosis.15- Prefer temporary scripts in the working directory or a project `diagnostics/` or `quality_reports/` folder only when the user permits artifacts.16- Do not install packages without approval. If a profiling package is unavailable, fall back to base R tools.17- Preserve project conventions from `CLAUDE.md`, `MEMORY.md`, `_quarto.yml`, renv files, and existing scripts.18- For analysis claims, distinguish observed runtime evidence from inferred causes.1920## Workflow21221. Identify the target code path, expected output, inputs, side effects, and package/session assumptions.232. Reproduce the issue with the smallest runnable harness:24 - source the function or script25 - load required inputs26 - set seeds and options when randomness or printing affects results27 - capture `sessionInfo()`, warnings, errors, and traceback283. Add edge-case tests based on the code:29 - zero rows, one row, all `NA`, mixed types, duplicate keys, missing columns, factor versus character, unusual names, date/time boundaries, and larger synthetic inputs304. Profile only after correctness is understood:31 - use `system.time()` for a baseline32 - use `Rprof()` and `summaryRprof()` for base-R profiling33 - use `bench`, `microbenchmark`, or `profvis` only when already available or approved345. Classify the issue:35 - correctness bug36 - dependency or environment mismatch37 - data-shape assumption38 - numerical or statistical edge case39 - memory pressure40 - vectorization or algorithmic bottleneck41 - rendering or Quarto execution failure426. Report the diagnosis with evidence and the smallest safe fix.4344## R Debugging Patterns4546Use base tools first:4748```r49tryCatch(50 source("script.R"),51 error = function(e) {52 message("ERROR: ", conditionMessage(e))53 traceback()54 },55 warning = function(w) {56 message("WARNING: ", conditionMessage(w))57 invokeRestart("muffleWarning")58 }59)6061sessionInfo()62```6364For profiling:6566```r67baseline <- system.time(source("script.R"))68print(baseline)6970Rprof("profile.out")71source("script.R")72Rprof(NULL)73print(summaryRprof("profile.out"))74```7576For edge-case checks, build a small table of test inputs and record actual versus expected behavior. Do not treat passing one happy-path run as sufficient when the requested problem concerns fragility.7778## Performance Triage7980Look specifically for:8182- growing objects in loops with `rbind`, `cbind`, or `c(...)`83- row-wise loops over data frames where vectorized or grouped operations are clearer84- repeated file I/O inside loops85- repeated joins, filters, or model fits that can be hoisted or cached86- implicit type conversions, factor level loss, and date parsing inside tight loops87- non-deterministic order from joins, grouping, or parallel execution88- memory-heavy copies of large data frames8990When recommending a refactor, show the before/after pattern only for the relevant lines and include timing evidence when possible.9192## Report Format9394Use this compact structure:9596```markdown97# R Debugging Report9899## Summary100- Target:101- Status: reproduced / not reproduced / partially reproduced102- Main cause:103- Proposed next action:104105## Evidence106| Check | Result | Notes |107|---|---|---|108109## Edge Cases110| Case | Expected | Actual | Status |111|---|---|---|---|112113## Profiling114| Path or function | Time or share | Evidence |115|---|---:|---|116117## Recommended Fixes118| Priority | Location | Fix | Rationale |119|---|---|---|---|120```121122If the user asks for implementation, apply only the approved or explicitly scoped edits and rerun the reproduction harness afterward.