Refactor Workflows
This was developed as I began using AI agents in creating the R package sysgenAnalysis based on analysis workflows developed in the Attie Lab. The goal was to refactor a set of analysis workflows, one at a time, into a modular R package structure.
The process starts with a single-file analysis workflow script, say [basename].R, for an analysis named [workflow]. Typically [basename].R has a mix of analysis steps, data cleaning and organization steps, and plotting steps. There may be several data objects used by the script, and the script may write several data objects and plots to files. Further, different scripts may share common functions.
Use: Be sure you are working in a folder where you want the new package components to live. Type the following prompt into AI, substituting for [basename].R and [workflow]
Prompt: “follow the workflow prompt to refactor [basename].R into a modular R package structure for the analysis [workflow]”.
Goal: Refactor a workflow into a modular R package structure.
Instructions:
- Set the workflow and basename variables from user input:
-
Understand the workflow: Read the workflow and understand what it does.
- Extract Functions: Move all logical units (data processing, analysis, plotting) into
R/[basename].R.- Document each function with Roxygen2 syntax (including
@param,@return, and@export). - Use R native pipes (
|>) and explicit namespace calls (e.g.,dplyr::mutate). - Centralize shared constants (like coordinate maps) if not already in
common.R.
- Document each function with Roxygen2 syntax (including
- Create Entry Script: Create an execution script
inst/scripts/analyze_[basename].R.- Use it as a clean entry point that calls the functions defined in
R/. - Handle environment setup, file paths, and high-level execution flow here.
- Use it as a clean entry point that calls the functions defined in
- Return S3 Objects: Refactor the main “run” function to return an S3 object of class
[basename]_analysis.- Implement
print,summary, andplotmethods inR/[basename].R. - Move file-saving logic (
write.csv,ggsave) out of the functions and into the entry script.
- Implement
- Verify Integration: Check package and scripts, correcting any issues that arise.
- Verify documents with
devtools::document(). - Build the package.
- Ask user whether or not to run the analyze_basename.R script to make sure it works.
- Verify documents with
- Create Exploration Document: Create an interactive Shiny-based Quarto document
inst/scripts/explore_[basename].qmdallowing for dynamic filtering and exploration.- Set
server: shinyin the YAML header. - Use
context: setupto load data viasysgenAnalysis::read_[basename]_analysis(). - Use
::: {.panel-tabset}to organize sections into Summary, Tables, and Plots. - Use Shiny’s
fluidRowandcolumnwithin each tab to create a side-bar like experience for controls next to outputs. - Integrate
downr::downloadUIanddownr::downloadServerfor exporting tables and plots. - This approach provides a premium, interactive experience while keeping rendering fast by loading pre-calculated CSVs.
- Set
Requested Files:
R/[basename].Rinst/scripts/analyze_[basename].Rinst/scripts/explore_[basename].qmd