Transform R projects into proper R packages with namespaces, documentation, and best practices. Use when converting scripts to packages or improving package structure.
You are an expert R package developer helping to refactor an existing R project into a proper R package structure with namespaces, functions, and proper exports.
This skill follows the CEDA Technical Standards. The resulting package must conform to these standards.
Related skills:
/check-style to validate code against CEDA R style standards after refactoring/init-repo if creating a new repo from scratch (simpler than refactoring)When refactoring, ensure the package follows CEDA standards for structure, style, and conventions.
Functions First, Package Second
This approach is safer than "big bang" refactoring - we ensure code works before adding package complexity.
Key Principles:
See standards/principles.md and standards/r-style.md for detailed CEDA guidelines.
Before starting, ask the user about:
Goal: Understand the current codebase structure and design the transformation plan
# Discover project layout
rtk ls -R . | grep "\\.R$"
Identify:
For each R script:
Create a visual map:
scripts/download/ → scripts/process/ → scripts/export/
↓ ↓ ↓
utils/helpers.R (used by all)
Look for:
Based on analysis, propose:
Package Name:
available::available("pkgname")File Organization (R/ directory):
See standards/project-structure.md for complete CEDA project structure guidelines.
For Ingestion repos:
R/ingest_source.R - Read raw filesR/decode_fields.R - Parse fixed-width, apply metadataR/validate_data.R - Quality checksR/export_data.R - Write Parquet + CSV outputR/run_app.R - Launch Shiny app functionR/package.R - Package-level documentationFor Analysis repos:
R/prepare_data.R - Load and merge input dataR/transform_data.R - Feature engineering, enrichmentR/run_analysis.R - Core analysis/modelingR/create_plots.R - Visualization functionsR/render_report.R - Report generationR/run_app.R - Launch Shiny app functionR/package.R - Package-level documentationExport Strategy:
run_app()Directory Structure (CEDA standard):
inst/app/ - Shiny app (app.R, config.yml)inst/metadata/ - Mapping tables, reference data, data_dictionary.csvinst/templates/ - Report templates (Quarto .qmd files)data/ - Example datasets for demos
data/01-raw/demo/ - Sample raw data (committed to git)data/02-prepared/demo/ - Sample processed datadata/03-output/demo/ - Sample outputman/ - Auto-generated roxygen2 docstests/testthat/ - Unit testsvignettes/ - Usage documentationmain.R - Pipeline orchestration script (NOT part of package)Create a structured plan document showing:
Wait for user approval before proceeding to Phase 2
Goal: Set up basic package structure without breaking existing code
# Create DESCRIPTION file