Skip to content
Skills Library
Programming · Learning & explanation

coding-in-r

/coding-in-r

How to write R well — renv, tidyverse/data.table, project paths, style, scripts, seeds. TRIGGER when authoring or editing .R/.Rmd/.qmd files. For running R on the Yale SOM HPC cluster (Slurm, renv on /gpfs), use running-r instead.

Licence
Unlicense
Last repository change
10.07.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
4
Deprecation

No signals detected. Repository changed on 10.07.2026, without references to retired models or APIs.

Licence

Unlicense, otwarta.

What it does and when to use it

How to write R well — renv, tidyverse/data.table, project paths, style, scripts, seeds. TRIGGER when authoring or editing .R/.Rmd/.qmd files. For running R on the Yale SOM HPC cluster (Slurm, renv on /gpfs), use running-r instead.

Original author description

How to write R well — renv, tidyverse/data.table, project paths, style, scripts, seeds. TRIGGER when authoring or editing .R/.Rmd/.qmd files. For running R on the Yale SOM HPC cluster (Slurm, renv on /gpfs), use running-r instead.

How it works

  1. Defaults
  2. Layout
  3. Paths
  4. Style
  5. Data
  6. CLI skeleton
  7. Checklist
  8. Further reading
Original SKILL.md headings · 110 lines, 372 words
SKILL.md preview
---
name: coding-in-r
description: How to write R well — renv, tidyverse/data.table, project paths, style, scripts, seeds. TRIGGER when authoring or editing .R/.Rmd/.qmd files. For running R on the Yale SOM HPC cluster (Slurm, renv on /gpfs), use running-r instead.
related:
  - programming-and-coding
  - running-r
  - accelerating-python
  - code-review
updated: 2026-05-22
---
# Coding in R

Rule: `renv`, portable paths, script as source of truth. Never rely on the global workspace.

For cluster execution, also load [running R](../running-r/SKILL.md).

## Defaults

- `renv` for package versions. Commit `renv.lock` and `.Rprofile`.
- On SOM HPC: `module load r`; no `rig` or `mise` default.
- `pak` for fast installs when available.
- `styler` for formatting.
- `lintr` for linting.
- `here::here()` for paths. No `setwd()` in tracked scripts.
- `readr::read_csv()` over base `read.csv()`.

```r
install.packages("renv")
renv::init()
renv::install(c("tidyverse", "data.table", "here", "fs", "styler", "lintr"))
renv::snapshot()
```

Restore with:

```r
renv::restore()
```

On HPC, restore once before arrays. Do not let many workers install packages concurrently.

## Layout

```text
project/
├── renv.lock
├── .Rprofile
├── README.md
├── scripts/       # entry points
├── R/             # shared helpers
├── notebooks/     # .Rmd / .qmd
├── data/raw/      # immutable; usually ignored
├── data/derived/  # rebuildable
└── results/       # figures/tables/logs
```

## Paths

Never commit `setwd()` or personal absolute paths.

```r
library(here)
counts_path <- here("data", "raw", "counts.csv")
counts <- readr::read_csv(counts_path)
```

For machine-specific roots, read an environment variable from `.Renviron` or the shell.

## Style

- `snake_case` for variables/functions.
- `<-` for assignment; `=` for arguments.
- Native pipe `|>` unless `%>%` materially helps.
- 2-space indentation; aim for 80-char lines, max 120.
- Named arguments: `mean(x, na.rm = TRUE)`, not `mean(x, T)`.
- `library()` at top.
- Never `attach()` or `<<-`.
- `set.seed(42)` before stochastic work.

## Data

Default to tidyverse for clarity. Use data.table when data are large, joins/grouping dominate, or in-place mutation matters. Benchmark before rewriting clear code for speed.

## CLI skeleton

```r
#!/usr/bin/env Rscript
suppressPackageStartupMessages({
  library(optparse)
})

option_list <- list(
  make_option(c("-i", "--input"), type = "character"),
  make_option(c("-o", "--output"), type = "character")
)
opt <- parse_args(OptionParser(option_list = option_list))
if (is.null(opt$input) || is.null(opt$output)) {
  stop("--input and --output are required", call. = FALSE)
}

set.seed(42)
```

## Checklist

- [ ] `styler::style_dir(".")` run
- [ ] `lintr::lint_dir(".")` clean or justified
- [ ] No `setwd()` or personal paths
- [ ] `set.seed()` where needed
- [ ] `renv::snapshot()` if deps changed
- [ ] Small smoke test run
- [ ] HPC uses `module load r` + `renv`, not `rig`/`mise`

## Further reading

- [Tidyverse style guide](https://style.tidyverse.org/)
- [renv](https://rstudio.github.io/renv/)
- [data.table](https://rdatatable.gitlab.io/data.table/)
- [here](https://here.r-lib.org/)

Versions

The database has no other skills named coding-in-r.

Database snapshot: 13.11.2026 · package version 4150761d3ae2 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 1 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 98coding-in-python
    How to write Python well — uv environments, formatting, CLI scripts, logging, seeds. TRIGGER when authoring or editing .py files. For running Python on the Yale SOM HPC cluster (sbatch, Slurm, uv on /gpfs), use running-python instead.
  • 100r-guide
    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…
  • 100running-r
    Run R on the Yale SOM HPC cluster with Lmod modules, renv, batch scripts, and BLAS/OpenMP thread control. TRIGGER when writing R Slurm jobs on the Yale SOM HPC cluster, using renv on the cluster, installing R packages on the cluster, or running Rscript in batch mode there.
  • 100rpix
    Use whenever R code lives in a Pixi project, meaning a pixi.toml (or a pyproject.toml with [tool.pixi]) next to .R, .qmd or .Rmd files or a DESCRIPTION, and when asked to set up an R project with Pixi or rpix, or to move one from renv. Covers installing and adding R packages…
  • 100r
    R language and document formats (R, Rmd, Quarto): coding standards and Copilot guidance for idiomatic, safe, and consistent code generation. Triggers on: **/*.R, **/*.r, **/*.Rmd, **/*.rmd, **/*.qmd
  • 100r-ops
    Modern R for data analysis and statistics — tidyverse-first (dplyr, tidyr, ggplot2, native |> pipe), with base R and data.table as alternatives. Triggers on: R, Rstats, tidyverse, dplyr, tidyr, ggplot2, pivot/join/group, data.table, purrr map, broom, renv, Quarto.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.