AgentSkillsCN

replication-package

为R项目使用rix/nix构建可重复的复制包。当您需要搭建项目环境、为期刊投稿准备代码,或确保计算过程的可重复性时,可选用此技能。涵盖rix、nix-shell、Makefile管道以及符合DCAS标准的文档编写。

SKILL.md
--- frontmatter
name: replication-package
description: Create reproducible replication packages using rix/nix for R projects. Use when setting up project environments, preparing code for journal submission, or ensuring computational reproducibility. Covers rix, nix-shell, Makefile pipelines, and DCAS-compliant documentation.

Replication Package with rix/nix

Initial Setup

Install nix

bash
# Linux/macOS
sh <(curl -L https://nixos.org/nix/install) --daemon

# Configure trusted users (add to /etc/nix/nix.custom.conf)
# trusted-users = root YOUR_USERNAME

# Restart daemon
sudo systemctl restart nix-daemon

Install cachix for faster builds

bash
nix-env -iA cachix -f https://cachix.org/api/v1/install
cachix use rstats-on-nix

Project Environment Setup

generate_env.R

r
library(rix)

# Define environment
rix(
  r_ver = "4.3.2",
  r_pkgs = c(
    # Data manipulation
    "data.table",
    "tidyverse",
    "here",
    
    # Econometrics
    "fixest",
    "did",
    "grf",
    "rdrobust",
    
    # Spatial
    "sf",
    "spdep",
    "spatialreg",
    
    # Tables and figures
    "modelsummary",
    "kableExtra",
    "ggplot2",
    "patchwork",
    
    # Utilities
    "furrr",
    "tictoc"
  ),
  git_pkgs = list(
    list(
      package_name = "concordance",
      repo_url = "https://github.com/insongkim/concordance",
      commit = "HEAD"
    )
  ),
  system_pkgs = c("quarto"),
  ide = "none",
  project_path = ".",
  overwrite = TRUE
)

Build and enter environment

bash
# Build the environment (first time takes a while)
nix-build

# Enter the shell
nix-shell

# Or combine
nix-shell --run "R"

Makefile for Pipeline

makefile
# Makefile for replication

.PHONY: all clean data analysis tables figures paper

all: paper

# Data construction
data/proc/analysis_data.rds: code/build/01-clean-data.R data/raw/*.csv
	nix-shell --run "Rscript code/build/01-clean-data.R"

data/proc/merged_data.rds: code/build/02-merge-data.R data/proc/analysis_data.rds
	nix-shell --run "Rscript code/build/02-merge-data.R"

# Analysis
output/tables/main_results.tex: code/analysis/01-main-regressions.R data/proc/merged_data.rds
	nix-shell --run "Rscript code/analysis/01-main-regressions.R"

output/figures/event_study.pdf: code/analysis/02-event-study.R data/proc/merged_data.rds
	nix-shell --run "Rscript code/analysis/02-event-study.R"

# Convenience targets
data: data/proc/merged_data.rds

analysis: output/tables/main_results.tex output/figures/event_study.pdf

tables: output/tables/main_results.tex

figures: output/figures/event_study.pdf

paper: output/paper/paper.pdf

output/paper/paper.pdf: output/paper/paper.qmd output/tables/*.tex output/figures/*.pdf
	nix-shell --run "quarto render output/paper/paper.qmd"

clean:
	rm -f data/proc/*.rds
	rm -f output/tables/*.tex
	rm -f output/figures/*.pdf

README Template (DCAS-compliant)

See references/readme-template.md for the full template. Key sections:

  1. Data Availability Statement: Source, access, licenses
  2. Computational Requirements: Hardware, software, runtime
  3. Description of Programs: What each script does
  4. Instructions: Step-by-step replication guide
  5. Output List: Map outputs to paper tables/figures

Folder Structure

code
project/
├── .here                    # Project root marker
├── .gitignore
├── Makefile
├── README.md
├── generate_env.R
├── default.nix              # Generated by rix
│
├── code/
│   ├── build/
│   │   ├── 01-clean-data.R
│   │   └── 02-merge-data.R
│   └── analysis/
│       ├── 01-main-regressions.R
│       └── 02-event-study.R
│
├── data/
│   ├── raw/                 # Original data (may be symlinked)
│   ├── build/               # Intermediate files
│   └── proc/                # Final analysis data
│
└── output/
    ├── tables/
    ├── figures/
    └── paper/

Symlinks for Large Data

bash
# Link Dropbox data folder to project
ln -s ~/Dropbox/Projects/MyProject/data data

# Link output to Overleaf (if using)
ln -s ~/Dropbox/Apps/Overleaf/MyPaper output/paper

.gitignore

gitignore
# Data (too large, stored elsewhere)
data/raw/*
data/build/*
data/proc/*
!data/raw/.gitkeep
!data/build/.gitkeep
!data/proc/.gitkeep

# Nix
result
.direnv/

# R
.Rhistory
.RData
.Rproj.user/

# Output (regenerated)
output/tables/*.tex
output/figures/*.pdf
output/figures/*.png

# OS
.DS_Store
Thumbs.db

Verification Checklist

Before submission:

  • nix-build completes without errors
  • make clean && make reproduces all outputs
  • README documents all data sources
  • All code files have headers with descriptions
  • Random seeds are set in all stochastic code
  • Runtime estimate is documented
  • Output matches paper tables/figures exactly