R language guardrails, patterns, and best practices for AI-assisted development.
Use when working with R files (.R, .Rmd, .qmd), DESCRIPTION, or when the user mentions R/RStudio.
Provides tidyverse conventions, vectorization patterns, R Markdown guidelines,
and testing standards specific to this project's coding standards.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Instruções da origem · Visualização somente leitura
name
r-guide
description
R language guardrails, patterns, and best practices for AI-assisted development.
Use when working with R files (.R, .Rmd, .qmd), DESCRIPTION, or when the user mentions R/RStudio.
Provides tidyverse conventions, vectorization patterns, R Markdown guidelines,
and testing standards specific to this project's coding standards.
Applies to: R 4.1+, Statistical Computing, Data Analysis, R Packages, Shiny Apps
Core Principles
Tidyverse First: Use tidyverse conventions for data manipulation, visualization, and functional programming; fall back to base R only when performance demands it
Vectorize Everything: Prefer vectorized operations and purrr::map() over explicit for loops; R is optimized for vector operations
Reproducibility: Every analysis must be reproducible -- use renv for dependency management, set.seed() for stochastic operations, and R Markdown/Quarto for literate programming
Functional Style: Write pure functions with no side effects; avoid modifying global state or relying on .GlobalEnv
Explicit Over Implicit: No reliance on partial matching, implicit type coercion, or positional argument passing for non-trivial functions
Guardrails
Version & Dependencies
Target R 4.1+ (native pipe |>, lambda shorthand \(x))
Manage dependencies with renv -- always commit renv.lock
For packages, declare all dependencies in DESCRIPTION (Imports:, Suggests:)
Pin CRAN snapshot dates in renv for full reproducibility
Run styler::style_pkg() and lintr::lint_package() before every commit
Naming: snake_case for functions/variables, PascalCase for R6/S4 classes
Max line length: 80 characters
Use <- for assignment (not = outside function arguments)
Explicit library() at top of scripts; never use require()
Always use TRUE/FALSE (never T/F -- they can be overwritten)
No attach() or setwd() -- use here::here() for project-relative paths
Vectorization
Prefer vectorized operations: x * 2 not for (i in seq_along(x)) x[i] * 2
Use dplyr::mutate() / dplyr::summarise() for column-wise transformations
Use purrr::map() family for list iteration (map_dbl(), map_chr(), map_dfr())
Use dplyr::across() for applying functions to multiple columns
Reserve for loops for side effects only (writing files, API calls)
Use vapply() over sapply() when base R is required (explicit return type)
Error Handling
Use rlang::abort() / cli::cli_abort() over stop() for structured conditions
Validate inputs at the start of every exported function
Use stopifnot() or rlang::arg_match() for argument validation
Never use try() -- always tryCatch() or purrr::safely()
validate_dataframe <-function(df, required_cols){if(!is.data.frame(df)){
cli::cli_abort("{.arg df} must be a data frame, not {.obj_type_friendly {df}}.")}
missing_cols <- setdiff(required_cols,names(df))if(length(missing_cols)>0){
cli::cli_abort("Missing required column{?s}: {.field {missing_cols}}.",class="validation_error")}invisible(df)}
Reproducibility
Always use set.seed() before stochastic operations; document the seed
Use renv::snapshot() after adding or updating packages
Never use absolute paths -- use here::here() for project-relative paths
Use R Markdown (.Rmd) or Quarto (.qmd) for analysis reports
Include sessioninfo::session_info() at the end of reports