R — 127 Operations for AI Agents
This page is the canonical reference an AI coding agent uses to refactor, query, and analyze R code through the act MCP server. 127 operations available: 53 refactor, 18 query, 42 analysis, 14 verification. Each operation is callable from Claude Code, Cursor, Codex, OpenCode, or any MCP-compatible agent host. Click any operation for a stable anchor link suitable for citation.
Worked R examples
act101 parses R with the tree-sitter grammar and edits function calls, if conditions, and namespace references as syntax nodes, so a qualifier or extracted condition lands on the exact call or expression it targets. It can rewrite an accumulating for loop as the equivalent purrr::map() call. It can replace a sprintf() format call with the equivalent paste0() concatenation. And it can pull a complex boolean expression out of an if() condition into a named variable declared just above it. Each example below is the verbatim output of the command shown, run against the file shown.
Rewrite an accumulating loop as purrr::map
analysis.R builds result by growing a vector inside a for loop, which reallocates on every iteration.
$ act refactor-lang convert_loop_to_purrr --file analysis.R --params '{"variant":"map","line":1,"column":1,"symbol":"result"}'
Before
result <- c()
for (x in items) {
result <- c(result, x * 2)
}
After
result <- purrr::map(items, function(x) x * 2)
The loop collapses into result <- purrr::map(items, function(x) x * 2); the result <- c() initialization line is removed with it.
Replace sprintf with paste0
greeting.R formats a message with sprintf and a %s placeholder.
$ act refactor-lang convert_sprintf_to_paste --file greeting.R --params '{"line":2,"column":1}'
Before
name <- "World"
msg <- sprintf("Hello, %s!", name)
After
name <- "World"
msg <- paste0("Hello, ", name, "!")
The call becomes paste0("Hello, ", name, "!"), splitting the format string at the placeholder and interleaving the argument.
Extract a compound if condition
range_check.R's if statement tests value > 0 && value < 100 inline.
$ act refactor-lang extract_condition --file range_check.R --params '{"variable_name":"condition","line":2,"column":1}'
Before
value <- 42
if (value > 0 && value < 100) {
result <- "in range"
}
After
value <- 42
condition <- value > 0 && value < 100
if (condition) {
result <- "in range"
}
The compound expression is assigned to condition on the line above, and the if now reads if (condition).
Query
18 query tools, the same on every supported language. Descriptions live in the shared reference: /docs/query-tools.
callers control_flow data_flow definition diagnostics effect_closure effect_summary fix_auto get_type graph import_organize interface mutations references repo_outline skeleton symbols symbols_batch
Refactor
| Operation | Description |
|---|---|
add-ellipsis-parameter |
Add ellipsis (...) parameter to a function for variable arguments |
add-function-documentation |
Add roxygen2 documentation block above a function |
add-namespace-prefix |
Add namespace prefix (pkg::) to a function call |
change-signature |
Modify function parameters, return, generics |
convert-aggregate-to-dplyr-group-by |
Convert aggregate() to group_by() %>% summarize() |
convert-baser-select-to-dplyr-select |
Convert df[, c('a', 'b')] to df %>% select(a, b) |
convert-baser-subset-to-dplyr-filter |
Convert df[df$col == x, ] to df %>% filter(col == x) |
convert-baser-transform-to-dplyr-mutate |
Convert df$new <- expr to df %>% mutate(new = expr) |
convert-double-negation |
Simplify !!x to x |
convert-for-to-vectorized |
Convert for loops to vectorized operations (sapply/lapply/Map) |
convert-function-to-pipe |
Convert sequential function calls to pipe chain |
convert-glue-to-sprintf |
Convert glue::glue() to sprintf() format strings |
convert-if-to-ifelse |
Convert scalar if-else to vectorized ifelse() |
convert-ifelse-chain-to-switch |
Convert if-else chains to switch() statements |
convert-ifelse-to-if |
Convert vectorized ifelse() to explicit if-else block |
convert-logical-operators |
Flatten &/| chains, apply De Morgan's laws |
convert-loop-to-apply |
Convert for loop to lapply/sapply pattern |
convert-loop-to-purrr |
Convert for loop to purrr::map_*() functional style |
convert-merge-to-dplyr-join |
Convert merge() to dplyr join functions |
convert-nested-to-pipe |
Convert nested function calls to magrittr pipe chains (%>%) |
convert-paste-to-glue |
Convert paste()/paste0() calls to glue::glue() string interpolation |
convert-pipe-to-nested |
Convert magrittr pipe chains (%>%) to nested function calls |
convert-reshape-to-pivot |
Convert reshape()/melt()/dcast() to tidyr pivot functions |
convert-sprintf-to-paste |
Convert sprintf() to paste() concatenation |
convert-switch-to-ifelse |
Convert switch() statement to if-else chain |
delete-function |
Delete a function definition |
delete-variable |
Delete a variable assignment |
extract-condition |
Extract complex condition to a named variable |
extract-function |
Extract code range to new function |
extract_function |
Extract a code selection into a new function — automatically infers parameters, return types, and inserts the call site. Use instead of manually cutting/pasting code. Works without LSP; LSP improves type inference. Params: file (string), new_name (string), start_line (u32), start_column (u32), end_line (u32), end_column (u32) [, preview (bool), receipt (bool)] |
extract_variable |
Extract an expression into a named variable — inserts the declaration and replaces the expression with the variable name. Works without LSP. Params: file (string), new_name (string), start_line (u32), start_column (u32), end_line (u32), end_column (u32) [, preview (bool)] |
generate-accessor-methods |
Generate get_/set_ accessor methods for R6 class fields |
generate-dataframe-constructor |
Generate data.frame() or tibble::tibble() constructor |
generate-function-skeleton |
Generate function skeleton with roxygen2 documentation |
generate-print-method |
Generate print.classname() method for S3 class |
generate-r-to-string-method |
Generate toString/as.character method for custom S3 class |
generate-roxygen-block |
Generate roxygen2 documentation block from function signature |
generate-test-function |
Generate testthat::test_that() skeleton |
inline |
Inline a variable, function, or method — replace every usage with its definition body, then remove the original. The inverse of extract. Works without LSP (single-file); LSP enables cross-file inlining. Params: file (string), symbol (string) [, line (u32), preview (bool), receipt (bool)] |
inline-variable |
Inline a variable by replacing references with its value |
insert_body |
Replace a function's implementation body with new code. AST-validated — rejects if the result has parse errors, so you can't accidentally break syntax. Use instead of manual text editing for function rewrites. Params: file (string), symbol (string), code (string) [, commit (bool)] |
introduce-local-variable |
Create local variable for repeated sub-expression |
invert-if-condition |
Invert if condition and swap branches |
move_symbol |
Move a function, class, or type to a different file and automatically update all imports across the codebase. Use instead of manually cut/paste + fixing imports. Works without LSP (single-file); LSP enables cross-file import updates. Params: file (string), symbol (string), destination (string) [, preview (bool), receipt (bool)] |
organize-library-calls |
Sort and deduplicate library() calls at the top of an R file |
recipe_run |
Run a codemod recipe: declarative match → transform → optional verify across modeled grammars. Preview lists matches; apply writes with optional E7 receipts and all-or-nothing rollback. Returns a per-site report. |
remove-namespace-prefix |
Remove redundant namespace prefix from function calls |
rename |
Rename a symbol and automatically update ALL references across the codebase. Safer and faster than find-and-replace — AST-aware, won't rename strings or comments. Works without LSP (single-file); LSP enables cross-file renames. Params: file (string), old_name (string), new_name (string) [, line (u32), column (u32), preview (bool), receipt (bool)] |
rename-dataframe-columns |
Rename data frame columns across all references |
rename-parameter |
Rename a function parameter and update all references |
replace-global-assignment |
Replace global assignment operator <<- with local <- |
simplify-function-signature |
Remove unused parameters from function signature |
wrap-in-trycatch |
Wrap code in tryCatch() with error handler |
Analysis
42 analysis tools, the same on every supported language. Descriptions live in the shared reference: /docs/analysis-tools.
analyze_api_diff analyze_chokepoints analyze_clones analyze_clusters analyze_cohesion analyze_conformance analyze_coupling analyze_cycle_risk analyze_cycles analyze_dead_code analyze_depth analyze_entry_points analyze_export analyze_extraction analyze_fan_balance analyze_features analyze_hotspots analyze_impact analyze_inconsistencies analyze_inheritance analyze_interface_bloat analyze_interfaces analyze_layers analyze_orphan_types analyze_patterns analyze_platform_deps analyze_readiness analyze_roles analyze_seams analyze_stability analyze_surface analyze_test_gaps analyze_thickness analyze_type_completeness churn_hotspots co_change_clusters coverage_overlay ownership_map profile_overlay simulate split_module trace_overlay
Verify
14 verify tools, the same on every supported language. Descriptions live in the shared reference: /docs/verification.
bisect_regression gate generate_test_harness scan secret_surface summarize_pr taint_flow unsafe_surface verify_behavioral_equivalence verify_contract_preserved verify_diff_semantics verify_port_parity verify_side_effects verify_test_impact