ams-r-pro
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 0
- Forks
- 1
03 SKILL.md 正文
R Pro
Overview
R's strength — expressive data analysis with a massive statistical ecosystem — turns into a liability when scripts grow into unmaintainable notebooks. Professional R means writing programs, not just scripts: vectorized operations, functions over copy-paste, packages over loose files, reproducible environments (renv), and reports that regenerate from raw data with one command.
The through-line: reproducible, vectorized, and packaged — analysis you can rerun and trust.
When to use
- Writing or reviewing R code for analysis, modeling, or reporting.
- Structuring R projects (packages, targets pipelines, renv).
- Choosing between base R and tidyverse idioms.
- Building Shiny apps or R Markdown/Quarto reports.
- Debugging performance or reproducibility issues.
Core concepts
- Vectorization is the language. Operations apply to whole vectors —
x * 2,ifelse,dplyr::mutate— loops over rows are almost always wrong (slow and unidiomatic). Think in columns and transformations, not iterations. - Tidyverse as a dialect.
dplyr(manipulation),tidyr(reshaping),ggplot2(visualization),purrr(functional iteration),readr/tibble(I/O). The pipe (|>) chains verbs readably. Pick tidyverse or base/data.table per project and stay consistent — mixing dialects confuses. - Tidy data. Each variable a column, each observation a row, each value a cell. Half of "R is
hard" is fighting untidy data —
pivot_longer/pivot_widerfirst, analysis second. - Functions and packages. Repeated analysis → functions; shared functions → a package
(
usethis::create_package) with documentation (roxygen2) and tests (testthat). Scripts are for exploration; packages are for reuse. - Reproducibility.
renvlocks package versions; Quarto/R Markdown weaves code + narrative so reports regenerate;targetsbuilds dependency-aware pipelines (changed upstream → rebuild only downstream). "It worked on my laptop" is not reproducibility. - Factors and types deliberately. Strings vs factors (know when each bites — modeling vs
display), dates as dates (
lubridate), missing values (NA) handled explicitly, not ignored.
Practical workflow
- Scaffold the project. RStudio project or plain dir +
renv::init();data/(raw, read-only),R/(functions),reports/(Quarto docs),tests/(testthat). Never modify raw data in place. - Explore interactively, then codify. Notebook exploration is fine — but promote repeated
logic into functions in
R/as soon as it's used twice. - Write vectorized pipelines.
read → clean (tidyr) → transform (dplyr) → model → visualize (ggplot2), each step a pipe chain or function. Avoid row loops; usepurrr::mapfamily for iteration that must happen. - Build the pipeline with
targets. Declare dependencies between steps;tar_make()rebuilds only what's stale. Your analysis becomes a build system, not a prayer. - Report with Quarto. Narrative + code + figures in one document; parameterized reports for repeated variants (per-region, per-month). The report is the analysis artifact.
- Test and review. testthat for package functions (edge cases: empty data, all-NA columns, unexpected types); code review for analysis code catches methodology bugs tests can't.
Idiomatic snippets:
library(dplyr)
orders |>
filter(!is.na(total), status != "cancelled") |>
mutate(month = lubridate::floor_date(created_at, "month")) |>
group_by(month, region) |>
summarise(
revenue = sum(total),
n_orders = n(),
.groups = "drop"
) |>
arrange(month)
# purrr instead of loops for model-per-group
models <- orders |>
split(.$region) |>
purrr::map(~ lm(total ~ discount, data = .x))
Common pitfalls
- Row-wise loops.
for (i in 1:nrow(df))— slow and unidiomatic. Vectorize, or usepurrr/dplyrverbs. (data.table is the legitimate fast alternative for huge data.) - Copy-paste analysis. The same 40 lines with different filters, diverging silently. Extract functions; parameterize reports.
- No environment lock. "Install latest tidyverse" in a README is not reproducibility.
renv::snapshot()and commit the lockfile. - Modifying raw data. Overwriting
data/raw.csvwith cleaned data destroys provenance. Raw is immutable; cleaning is code. - Factors biting. Unexpected level ordering in plots/models, or
read.csvcoercing strings. Be deliberate: set levels explicitly, usestringsAsFactorsintentionally. - Growing vectors in loops.
x <- c(x, new)reallocates every iteration — quadratic. Preallocate or collect in a list and combine once. - Shiny reactivity spaghetti. Observers triggering observers, no modularization. Shiny modules for reusable components; keep server logic in functions testable outside Shiny.
04 同仓库更多 Skills
web-scrape
Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.
design-taste-frontend
tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
systematic-debugging
Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes
requesting-code-review
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).