Skill: jackkuo666/running-r-analysis-in-existing-projects (version 0.1.0)

--- name: running-r-analysis-in-existing-projects description: Work inside an existing R project to extend analyses, modify scripts, run statistical models, update visualizations, and regenerate reports. --- # Running R Analysis in Existing Projects This skill operates inside an already structured R project. It helps extend, debug, or enhance existing analyses without recreating the project from scratch. Use this skill when the user wants to: - Continue analysis in an existing R project - Modify or extend R scripts - Add new statistical models or tests - Update plots or figures - Regenerate reports after data or code changes - Debug R errors in a project --- ## What This Skill Does When activated, this skill will: 1. **Understand the project structure** - Detect folders like `data/`, `scripts/`, `results/`, `reports/` - Identify `.Rproj`, `.Rmd`, `.qmd`, or `.R` files 2. **Inspect existing analysis** - Read current scripts and reports - Identify which packages and methods are being used - Avoid rewriting working components unnecessarily 3. **Extend or modify analysis** - Add new models or statistical tests - Introduce new plots using `ggplot2` - Add new data processing steps - Improve code structure or reproducibility 4. **Re-run and update outputs** - Recompute results - Overwrite or version new outputs in `results/` - Re-render R Markdown or Quarto reports 5. **Debug issues** - Fix missing packages - Resolve file path problems - Handle common R errors and warnings --- ## Example User Requests That Should Trigger This Skill - "Add a survival analysis to this R project" - "Update the plots in my report" - "This R Markdown file throws an error, fix it" - "Extend this analysis with a mixed-effects model" - "Re-run everything after I updated the data" --- ## Example Workflow **User:** Add a logistic regression model and update the report. **Skill actions:** - Locate main analysis script - Add logistic regression using `glm()` - Save model summary to `results/` - Update report with new section and plot - Re-render HTML/PDF report --- ## Tools & Packages Commonly Used | Purpose | R Packages | |--------|------------| | Data wrangling | tidyverse, dplyr | | Modeling | stats, lme4, glmnet | | Visualization | ggplot2 | | Reporting | rmarkdown, quarto | | Project management | here, renv | --- ## Notes - Respect the existing project structure and style - Do not delete user code unless explicitly requested - Prefer incremental updates over full rewrites - Always regenerate reports after modifying analysis

path: README.md
content: # Running R Analysis in Existing Projects

A Claude Code skill for extending, modifying, and debugging analyses within existing R projects.

## Features

- 🔍 **Understand Existing Code** - Reads and analyzes current scripts and reports
- ➕ **Incremental Extensions** - Adds new models, tests, and visualizations
- 🐛 **Debug Errors** - Fixes common R errors and missing packages
- 🔄 **Regenerate Reports** - Re-renders R Markdown/Quarto after changes
- 📦 **Respects Structure** - Works within existing project conventions
- 📊 **Update Visualizations** - Enhances or creates new ggplot2 figures

## Quick Start

Navigate to your existing R project and invoke the skill:

```
Add a survival analysis to this R project
```

```
Update the plots in my report with violin plots
```

```
This R Markdown file throws an error, fix it
```

```
Extend this analysis with a mixed-effects model
```

## Project Structure

The skill works with standard R project structures:

```
my-r-project/
├── data/
│   ├── raw/               # Original, immutable data files
│   └── processed/         # Cleaned, transformed data
├── scripts/               # R analysis scripts
├── results/
│   ├── figures/           # Plots and visualizations
│   ├── tables/            # Summary tables
│   └── models/            # Saved model objects
├── reports/               # R Markdown/Quarto documents
├── renv.lock              # Package version lock file
└── myproject.Rproj        # RStudio project file
```

## Usage Examples

### Example 1: Adding New Analysis

```
Add a logistic regression model to predict outcomes
```

The skill will:
- Read existing scripts to understand data structure
- Add `glm()` model with appropriate formula
- Save model summary to `results/models/`
- Create diagnostic plots
- Update report with new section

### Example 2: Updating Visualizations

```
Replace the boxplots with violin plots and add significance tests
```

The skill will:
- Locate existing plotting code
- Replace `geom_boxplot()` with `geom_violin()`
- Add statistical annotations (e.g., `stat_compare_means()`)
- Regenerate figures in `results/figures/`
- Re-render affected reports

### Example 3: Debugging

```
This script has an error when loading the data
```

The skill will:
- Read the error message
- Identify the issue (file path, missing package, etc.)
- Fix the code
- Verify the fix works

### Example 4: Data Update Workflow

```
I added new rows to the CSV, re-run everything
```

The skill will:
- Re-run data loading scripts
- Recompute all analyses
- Regenerate all plots
- Re-render reports with updated results

## Common Workflows

### Extending Analysis

```r
# Skill will add to existing scripts
# Example: Adding correlation analysis
cor_result <- cor.test(df$value1, df$value2)
# Save results
write_csv(as.data.frame(cor_result$estimate),
          here("results", "tables", "correlation.csv"))
```

### Updating Reports

```bash
# Skill re-renders after code changes
quarto render reports/analysis_report.qmd
# or
rmarkdown::render("report.Rmd")
```

### Installing Missing Packages

```r
# Skill detects and installs missing packages
install.packages("lme4")  # For mixed models
# Or Bioconductor
BiocManager::install("DESeq2")
```

## Supported Extensions

This skill can add:

- **Models**: Linear, logistic, mixed-effects, survival, GLM
- **Tests**: ANOVA, chi-square, Wilcoxon, correlation
- **Visualizations**: Violin plots, heatmaps, scatter plots, multi-panel figures
- **Data Processing**: Filtering, transformations, feature engineering
- **Report Sections**: Methods, discussion, appendices

## Best Practices

1. **Read before writing** - Understand existing code structure first
2. **Respect conventions** - Match existing coding style and folder organization
3. **Incremental changes** - Add without rewriting working code
4. **Test after changes** - Verify code runs before updating reports
5. **Document additions** - Add comments explaining new code

## Requirements

- Existing R project with standard structure
- R >= 4.0.0
- Required packages vary by analysis type

## Related Skills

- `creating-r-research-projects` - For starting new R projects from scratch

## License

MIT


path: examples/README.md
content: # Examples for Running R Analysis in Existing Projects

This directory contains example R projects that demonstrate how the skill works with already-established projects.

## Existing Project Demo

The `existing-project-demo/` contains a basic R project with:
- Sample dataset (`data/sample_data.csv`)
- Basic analysis script (`scripts/basic_analysis.R`)
- Simple Quarto report (`reports/basic_report.qmd`)

This serves as a starting point to demonstrate how the skill can:
- Add new statistical models and tests
- Extend visualizations
- Update and regenerate reports
- Debug existing code
- Incorporate new data

See the demo's README.md for detailed examples.


path: existing-project-demo/README.md
content: # Example: Extending an Existing R Project

This example demonstrates how the `running-r-analysis-in-existing-projects` skill works with an already-established R project.

## The Existing Project

This is a **basic** analysis project that contains:
- A simple dataset with control/treatment groups
- Basic t-test analysis
- A single boxplot visualization
- A basic Quarto report

## What This Skill Would Do

When activated in this project, the skill can:

### 1. Add New Analysis
- "Add correlation analysis between value1 and value2"
- "Include ANOVA for multiple group comparisons"
- "Add effect size calculations"

### 2. Extend Visualizations
- "Create a scatter plot showing value1 vs value2"
- "Add violin plots to compare distributions"
- "Create a combined multi-panel figure"

### 3. Enhance Reports
- "Update the report with new correlation section"
- "Add a methods section"
- "Include interactive plots with plotly"

### 4. Debug Issues
- "Fix the error when running the analysis"
- "Install missing packages"

### 5. Update After Data Changes
- "Re-run everything after I added new rows to the data"
- "Regenerate all plots with updated data"

## Example Workflow

**User request:** "Add a correlation analysis and scatter plot, then update the report."

**Skill actions:**
1. Reads `scripts/basic_analysis.R` to understand current approach
2. Adds correlation test (`cor.test()`) and scatter plot code
3. Creates new output file `results/correlation_analysis.csv`
4. Saves scatter plot to `results/scatterplot.png`
5. Updates `reports/basic_report.qmd` with new section
6. Re-renders the report to HTML

## Key Difference from Creating New Projects

| Creating New Projects | Extending Existing Projects |
|----------------------|----------------------------|
| Starts from scratch | Works with existing code |
| Creates all folders | Uses existing structure |
| Sets up dependencies | Adds to existing dependencies |
| Writes first draft | Extends/modifies existing code |
| "Create an R project..." | "Add X to this R project..." |


path: existing-project-demo/data/sample_data.csv
content: id,group,value1,value2
1,control,5.2,3.1
2,control,4.8,2.9
3,control,5.5,3.3
4,control,4.9,3.0
5,control,5.1,3.2
6,treatment,7.2,4.5
7,treatment,6.8,4.1
8,treatment,7.5,4.7
9,treatment,7.0,4.3
10,treatment,7.3,4.6


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: suspicious | SkillSpector: suspicious (MEDIUM)