From d33f7046c6dca901adb6a0fbf669ef8bb7a81db8 Mon Sep 17 00:00:00 2001 From: lukabor Date: Mon, 8 Jun 2026 23:02:26 +0200 Subject: [PATCH] add skill: scaffold/build-r-analysis (renv + targets + Quarto + rocker/verse) --- .../scaffold/build-r-analysis/Dockerfile.tpl | 37 +++++++++++ .../R-pipeline-load_data.R.tpl | 12 ++++ skills/scaffold/build-r-analysis/SKILL.md | 63 +++++++++++++++++++ .../scaffold/build-r-analysis/_targets.R.tpl | 11 ++++ .../build-r-analysis/docker-compose.yml.tpl | 22 +++++++ .../scaffold/build-r-analysis/gitignore.tpl | 15 +++++ skills/scaffold/build-r-analysis/justfile.tpl | 61 ++++++++++++++++++ .../build-r-analysis/reports-index.qmd.tpl | 11 ++++ .../tests-test-functions.R.tpl | 14 +++++ 9 files changed, 246 insertions(+) create mode 100644 skills/scaffold/build-r-analysis/Dockerfile.tpl create mode 100644 skills/scaffold/build-r-analysis/R-pipeline-load_data.R.tpl create mode 100644 skills/scaffold/build-r-analysis/SKILL.md create mode 100644 skills/scaffold/build-r-analysis/_targets.R.tpl create mode 100644 skills/scaffold/build-r-analysis/docker-compose.yml.tpl create mode 100644 skills/scaffold/build-r-analysis/gitignore.tpl create mode 100644 skills/scaffold/build-r-analysis/justfile.tpl create mode 100644 skills/scaffold/build-r-analysis/reports-index.qmd.tpl create mode 100644 skills/scaffold/build-r-analysis/tests-test-functions.R.tpl diff --git a/skills/scaffold/build-r-analysis/Dockerfile.tpl b/skills/scaffold/build-r-analysis/Dockerfile.tpl new file mode 100644 index 0000000..37e3b9d --- /dev/null +++ b/skills/scaffold/build-r-analysis/Dockerfile.tpl @@ -0,0 +1,37 @@ +FROM rocker/verse:4.4 AS base + +RUN apt-get update && apt-get install -y --no-install-recommends \ + libcurl4-openssl-dev \ + libssl-dev \ + libxml2-dev \ + && rm -rf /var/lib/apt/lists/* + +RUN install2.r --error --skipinstalled renv + +WORKDIR /usr/ + +ENV RENV_PATHS_LIBRARY=renv/library + +# ── production ────────────────────────────────────────────────────────── +FROM base AS production + +COPY renv.lock renv.lock +COPY renv/activate.R renv/activate.R + +RUN R -e "renv::restore()" + +COPY . . + +CMD ["R", "--vanilla", "-e", "targets::tar_make()"] + +# ── development ──────────────────────────────────────────────────────── +FROM base AS development + +COPY renv.lock renv.lock +COPY renv/activate.R renv/activate.R + +RUN R -e "renv::restore()" + +COPY . . + +CMD ["bash"] diff --git a/skills/scaffold/build-r-analysis/R-pipeline-load_data.R.tpl b/skills/scaffold/build-r-analysis/R-pipeline-load_data.R.tpl new file mode 100644 index 0000000..ea5c434 --- /dev/null +++ b/skills/scaffold/build-r-analysis/R-pipeline-load_data.R.tpl @@ -0,0 +1,12 @@ +box::use( + fs[dir_ls], + purrr[map, list_rbind], + readr[read_csv], +) + +#' @export +load_raw_data <- function(path) { + dir_ls(path, glob = "*.csv") |> + map(read_csv) |> + list_rbind() +} diff --git a/skills/scaffold/build-r-analysis/SKILL.md b/skills/scaffold/build-r-analysis/SKILL.md new file mode 100644 index 0000000..a78214c --- /dev/null +++ b/skills/scaffold/build-r-analysis/SKILL.md @@ -0,0 +1,63 @@ +--- +name: build-r-analysis +description: Scaffold a new R analysis project with renv for dependency management, three-stage Docker on rocker/verse (base/production/development), docker-compose, justfile, a `_targets.R` workflow skeleton, and Quarto reports under `reports/`. Configured with lintr, styler, testthat, and box. Materialises files and stops on `jj diff` for review before any commit. Use when the user says "new R analysis", "scaffold R project", "create R analysis", "set up R data science project", "new quarto project", "/build-r-analysis". +--- + +# build-r-analysis + +## When to use +- User says "new R analysis", "scaffold R analysis", "/build-r-analysis". +- A fresh directory needs renv + targets + Quarto on rocker/verse. + +## Inputs to collect (one grouped prompt) +- **Project name** (snake_case). +- **One-line description**. + +## Process + +1. **Confirm target directory** is empty (or user-confirmed override). +2. **Materialise files** from sibling templates: + ``` + / + ├── R/functions/.gitkeep + ├── R/pipeline/load_data.R ← R-pipeline-load_data.R.tpl + ├── data/raw/.gitkeep + ├── data/processed/.gitkeep + ├── reports/index.qmd ← reports-index.qmd.tpl + ├── tests/testthat/test-functions.R ← tests-test-functions.R.tpl + ├── _targets.R ← _targets.R.tpl + ├── Dockerfile ← Dockerfile.tpl + ├── docker-compose.yml ← docker-compose.yml.tpl + ├── justfile ← justfile.tpl + └── .gitignore ← gitignore.tpl + ``` +3. **Stop. Do not run `jj describe`.** Tell the user: + > "Review with `jj diff`. Then `just build-dev` → `just up` → `R -e + > 'renv::init()'` to lock dependencies. Then `just run` to execute + > the pipeline. Describe with + > [`/jj-atomic`](../../productivity/jj-atomic/SKILL.md) when satisfied." + +## Placeholders used by templates +- `` — snake_case project name. +- `` — one-line description (for `reports/index.qmd`). + +## Guardrails +- **Never** scaffold into a non-empty directory without confirmation. +- **Never** invoke `jj describe` — first commit is the user's call. +- **Never** invoke `renv::init()` for the user. +- **`R/functions/` and `R/pipeline/` follow `box::use` rules** — + no `library()`, no `source()`. See + [`../../engineering/r-style/box.md`](../../engineering/r-style/box.md). +- **Skeleton `_targets.R` has two demo targets** (`raw`, `processed`) + showing the box-imported pipeline pattern. Replace, don't extend. + +## See also +- [`../../_concepts/three-stage-dockerfile.md`](../../_concepts/three-stage-dockerfile.md) + — rationale for `Dockerfile.tpl`. +- [`../../engineering/dev-r/SKILL.md`](../../engineering/dev-r/SKILL.md) + — workflow auto-detects `_targets.R` → R-analysis loop. +- [`../../engineering/r-style/SKILL.md`](../../engineering/r-style/SKILL.md) + — every function in `R/functions/` and `R/pipeline/` follows these. +- [`../../engineering/tdd-bdd/SKILL.md`](../../engineering/tdd-bdd/SKILL.md) + — `tests/testthat/` opens with the standard `box::use(testthat[...])` + prologue (see `tdd-bdd/tests-r.md`). diff --git a/skills/scaffold/build-r-analysis/_targets.R.tpl b/skills/scaffold/build-r-analysis/_targets.R.tpl new file mode 100644 index 0000000..4c58e94 --- /dev/null +++ b/skills/scaffold/build-r-analysis/_targets.R.tpl @@ -0,0 +1,11 @@ +box::use(targets[tar_option_set, tar_target]) +box::use( + ./R/pipeline/load_data[load_raw_data], +) + +tar_option_set(packages = character(0)) + +list( + tar_target(raw, load_raw_data("data/raw/")), + tar_target(processed, raw) +) diff --git a/skills/scaffold/build-r-analysis/docker-compose.yml.tpl b/skills/scaffold/build-r-analysis/docker-compose.yml.tpl new file mode 100644 index 0000000..0ec2e0d --- /dev/null +++ b/skills/scaffold/build-r-analysis/docker-compose.yml.tpl @@ -0,0 +1,22 @@ +services: + dev: + build: + context: . + dockerfile: Dockerfile + target: development + container_name: -dev + volumes: + - .:/usr/ + - renv-cache:/root/.cache/R/renv + + production: + build: + context: . + dockerfile: Dockerfile + target: production + container_name: -prod + volumes: + - .:/usr/ + +volumes: + renv-cache: diff --git a/skills/scaffold/build-r-analysis/gitignore.tpl b/skills/scaffold/build-r-analysis/gitignore.tpl new file mode 100644 index 0000000..ccfc46c --- /dev/null +++ b/skills/scaffold/build-r-analysis/gitignore.tpl @@ -0,0 +1,15 @@ +.Rproj.user +.Rhistory +.Rdata +.DS_Store +*.Rproj +renv/library/ +renv/local/ +renv/cellar/ +renv/lock/ +renv/python/ +renv/staging/ +_targets/ +data/processed/ +reports/*.html +reports/*.pdf diff --git a/skills/scaffold/build-r-analysis/justfile.tpl b/skills/scaffold/build-r-analysis/justfile.tpl new file mode 100644 index 0000000..19f000e --- /dev/null +++ b/skills/scaffold/build-r-analysis/justfile.tpl @@ -0,0 +1,61 @@ +# — task runner +# `just --list` for all recipes. + +set dotenv-load := false + +default: + @just --list + +# ── Docker ────────────────────────────────────────────────────────────── +build-dev: + docker compose build dev + +build-prod: + docker compose build production + +up: + docker compose run --rm dev bash + +down: + docker compose down + +rebuild: + docker compose build --no-cache dev + +# ── Pipeline ──────────────────────────────────────────────────────────── +run: + docker compose run --rm dev Rscript -e "targets::tar_make()" + +status: + docker compose run --rm --no-deps dev Rscript -e "targets::tar_visnetwork()" + +rerun: + docker compose run --rm dev Rscript \ + -e "targets::tar_invalidate(everything()); targets::tar_make()" + +# ── Reports ───────────────────────────────────────────────────────────── +render: + docker compose run --rm dev quarto render reports/ + +# ── Linting & Formatting ──────────────────────────────────────────────── +lint: + docker compose run --rm --no-deps dev Rscript \ + -e "lintr::lint_dir('R/')" + +style: + docker compose run --rm --no-deps dev Rscript \ + -e "styler::style_dir('R/')" + +style-check: + docker compose run --rm --no-deps dev Rscript \ + -e "styler::style_dir('R/', dry = 'on')" + +# ── Testing ───────────────────────────────────────────────────────────── +test: + docker compose run --rm --no-deps dev Rscript \ + -e "testthat::test_dir('tests/testthat')" + +# ── Combined ──────────────────────────────────────────────────────────── +check: lint style-check test + +fix-and-check: style run test diff --git a/skills/scaffold/build-r-analysis/reports-index.qmd.tpl b/skills/scaffold/build-r-analysis/reports-index.qmd.tpl new file mode 100644 index 0000000..512d7a8 --- /dev/null +++ b/skills/scaffold/build-r-analysis/reports-index.qmd.tpl @@ -0,0 +1,11 @@ +--- +title: "" +subtitle: "" +format: html +execute: + echo: false +--- + +```{r setup, include=FALSE} +box::use(targets[tar_read]) +``` diff --git a/skills/scaffold/build-r-analysis/tests-test-functions.R.tpl b/skills/scaffold/build-r-analysis/tests-test-functions.R.tpl new file mode 100644 index 0000000..82ab481 --- /dev/null +++ b/skills/scaffold/build-r-analysis/tests-test-functions.R.tpl @@ -0,0 +1,14 @@ +project_root <- normalizePath(file.path( + testthat::test_path(), "..", ".." +)) +options(box.path = project_root) + +box::use( + testthat[describe, it, expect_true] +) + +describe("placeholder", { + it("passes", { + expect_true(TRUE) + }) +})