code-review
Review code for security, quality, and best practices. Use after writing code and before committing.
Test-driven development workflow for R. Write tests first, then implement. Use for new features, bug fixes, and refactoring.
> /plugin marketplace add ab604/claude-code-r-skillsHow it fires
How this command gets triggered: by you, by Claude, or both.
/tddContext preview
What this command does when you run it.
Test-driven development workflow for R. Write tests first, then implement. Use for new features, bug fixes, and refactoring.
name: tdd description: Test-driven development workflow for R. Write tests first, then implement. Use for new features, bug fixes, and refactoring.
Follow the TDD workflow: write tests first, then implement code to make them pass.
**RED → GREEN → REFACTOR**
1. **RED** - Write a failing test 2. **GREEN** - Write minimal code to pass 3. **REFACTOR** - Improve while keeping tests green
# What should the function do? # rescale01: Rescale a numeric vector to [0, 1] range # - Minimum value maps to 0 # - Maximum value maps to 1 # - Handle NA values appropriately
# tests/testthat/test-rescale.R
library(testthat)
test_that("rescale01 maps to [0, 1] range", {
expect_equal(rescale01(c(0, 5, 10)), c(0, 0.5, 1))
expect_equal(rescale01(c(-10, 0, 10)), c(0, 0.5, 1))
})
test_that("rescale01 handles edge cases", {
expect_equal(rescale01(c(5, 5, 5)), c(NaN, NaN, NaN))
expect_equal(rescale01(numeric(0)), numeric(0))
})
test_that("rescale01 handles NA values", {
expect_equal(rescale01(c(0, NA, 10)), c(0, NA, 1))
})devtools::test() # ✖ rescale01 maps to [0, 1] range # ✖ rescale01 handles edge cases # ✖ rescale01 handles NA values
# R/rescale.R
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}devtools::test() # ✔ rescale01 maps to [0, 1] range # ✔ rescale01 handles edge cases # ✔ rescale01 handles NA values
Improve code while keeping tests green:
rescale01 <- function(x, na.rm = TRUE) {
rng <- range(x, na.rm = na.rm, finite = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}covr::package_coverage() # rescale01: 100% coverage
| Code Type | Minimum Coverage | |-----------|-----------------| | General code | 80% | | Statistical calculations | 100% | | Data validation | 100% | | Security functions | 100% | | Core business logic | 100% |
test_that("clean_data removes invalid rows", {
input <- tibble(
id = 1:4,
value = c(1, NA, 3, -999)
)
result <- clean_data(input)
expect_equal(nrow(result), 2)
expect_equal(result$id, c(1, 3))
})test_that("fit_model returns expected structure", {
data <- tibble(x = 1:10, y = 2 * 1:10 + rnorm(10))
model <- fit_model(data, y ~ x)
expect_s3_class(model, "lm")
expect_named(coef(model), c("(Intercept)", "x"))
})test_that("validate_input throws informative errors", {
expect_error(
validate_input(NULL),
class = "validation_error"
)
expect_snapshot(
validate_input("not numeric"),
error = TRUE
)
})**DON'T:**
**DO:**
# All tests
devtools::test()
# With coverage
covr::package_coverage()
# Specific file
testthat::test_file("tests/testthat/test-rescale.R")
# Watch mode
testthat::auto_test_package()**Remember: Tests are not optional. Write them FIRST.**
A curated collection of Claude Code configurations for modern R use. These skills, rules, commands, and agents help Claude Code understand R best practices and generate idiomatic, high-quality R code.
Repo: ab604/claude-code-r-skills
Review code for security, quality, and best practices. Use after writing code and before committing.
Create an implementation plan before writing code. Use for new features, architectural changes, or complex refactoring.
Run full R verification loop before committing. Checks build, lint, style, test coverage, and code quality.