add skill: scaffold/build-r-analysis (renv + targets + Quarto + rocker/verse)

This commit is contained in:
Łukasz Boryń 2026-06-08 23:02:26 +02:00
parent 356fb7fce7
commit d33f7046c6
9 changed files with 246 additions and 0 deletions

View File

@ -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/<project-name>
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"]

View File

@ -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()
}

View File

@ -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:
```
<project>/
├── 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
- `<project-name>` — snake_case project name.
- `<description>` — 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`).

View File

@ -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)
)

View File

@ -0,0 +1,22 @@
services:
dev:
build:
context: .
dockerfile: Dockerfile
target: development
container_name: <project-name>-dev
volumes:
- .:/usr/<project-name>
- renv-cache:/root/.cache/R/renv
production:
build:
context: .
dockerfile: Dockerfile
target: production
container_name: <project-name>-prod
volumes:
- .:/usr/<project-name>
volumes:
renv-cache:

View File

@ -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

View File

@ -0,0 +1,61 @@
# <project-name> — 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

View File

@ -0,0 +1,11 @@
---
title: "<project-name>"
subtitle: "<description>"
format: html
execute:
echo: false
---
```{r setup, include=FALSE}
box::use(targets[tar_read])
```

View File

@ -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)
})
})