Workflow Prompt
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 your file xxx.R and desired base name yyy
Prompt: “follow the workflow prompt to refactor workflow = xxx.R into a modular R package structure with basename = yyy”.
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