r-package
Develop R packages following best practices. Use when creating packages, writing package functions, documentation, or tests.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Develop R packages following best practices. Use when creating packages, writing package functions, documentation, or tests.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
Write R code following tidyverse style guide. Use when writing R functions, scripts, or any R code.
Access R package documentation and learn about packages. Use when exploring packages, finding function help, or understanding package capabilities.
Explore and inspect R data frames, objects, and environments interactively. Use when the user wants to understand data structure, preview data, or investigate R objects.
rlang metaprogramming patterns for data-masking, injection operators, and dynamic dots. Use when writing functions that use tidy evaluation.
SOC 직업 분류 기준
| name | r-package |
| description | Develop R packages following best practices. Use when creating packages, writing package functions, documentation, or tests. |
Build R packages following tidyverse and CRAN best practices.
mypkg/
├── DESCRIPTION
├── NAMESPACE
├── R/
│ └── *.R
├── man/
│ └── *.Rd
├── tests/
│ └── testthat/
├── vignettes/
└── inst/
library() or require() in package codepkg::fun() syntax for external functionsdevtools::check() before committingImports:
dplyr (>= 1.0.0),
rlang,
cli
# Good - explicit namespace
my_function <- function(df) {
df |>
dplyr::filter(x > 0) |>
dplyr::mutate(y = x * 2)
}
# Bad - relies on attached package
my_function <- function(df) {
df |>
filter(x > 0) |>
mutate(y = x * 2)
}
#' Calculate the mean of positive values
#'
#' @param x A numeric vector.
#' @param na.rm Logical. Remove NA values? Default `TRUE`.
#'
#' @return A single numeric value.
#' @export
#'
#' @examples
#' positive_mean(c(-1, 2, 3, NA))
positive_mean <- function(x, na.rm = TRUE) {
x <- x[x > 0]
mean(x, na.rm = na.rm)
}
@export - make function available to users@param - document each parameter@return - describe return value@examples - runnable examples@noRd - internal function, no documentation@keywords internal - document but hide from indexpositive_mean.RR/utils.R for small utilities used across functionsUse rlang/cli for informative errors:
my_function <- function(x) {
if (!is.numeric(x)) {
cli::cli_abort(
"{.arg x} must be numeric, not {.obj_type_friendly {x}}."
)
}
# ...
}
Use withr for temporary state changes:
# Good
my_function <- function() {
withr::local_options(list(digits = 3))
withr::local_envvar(c(TZ = "UTC"))
# code runs with modified state
# state automatically restored on exit
}
# Bad
my_function <- function() {
old <- options(digits = 3)
on.exit(options(old))
# ...
}
tests/
├── testthat.R
└── testthat/
├── test-positive-mean.R
└── test-utils.R
testthat::test_that("positive_mean calculates correctly", {
testthat::expect_equal(positive_mean(c(1, 2, 3)), 2)
testthat::expect_equal(positive_mean(c(-1, 2, 4)), 3)
testthat::expect_equal(positive_mean(c(-1, -2)), NaN)
})
testthat::test_that("positive_mean handles NA", {
testthat::expect_equal(positive_mean(c(1, NA, 3)), 2)
testthat::expect_equal(positive_mean(c(1, NA, 3), na.rm = FALSE), NA_real_)
})
testthat::test_that("positive_mean errors on non-numeric", {
testthat::expect_error(positive_mean("a"), "must be numeric")
})
Code outside functions runs at build time, not when users call functions:
# Bad - path cached at build time
data_path <- system.file("data", package = "mypkg")
# Good - path resolved at call time
get_data_path <- function() {
system.file("data", package = "mypkg")
}
# Load all package code for interactive testing
pkgload::load_all()
# Run tests (when in an R package)
testthat::test_package()
# Check package
1. Build package in terminal `R CMD BUILD <pkg-directory>`
2. Check package with `R CMD check <pkg>_<version>.tar.gz`
# Build documentation
roxygen2::roxygenize()
# Install package locally
renv::install()
Depends on packages (use Imports)\dontrun{} for examples that can't run on CRAN--as-cran flaglibrary() or require() in R/ filessource() to load codesetwd()::: to access internal functions from other packages@return in documentation