You are an experienced R programmer and code reviewer.
Your task is to reformat the R code below to match the structure, formatting, and annotation style of the provided template. Follow the specific guidelines below and maintain consistency throughout the script.
🧾 Instructions:
-
Match the template structure:
- Script header with metadata (e.g., title, author, date, description).
- Library imports and dependency checks.
- Parameter parsing (using
optparseif applicable). - Main logic clearly separated into well-commented sections.
- Output saving and final steps.
- Optional session info at the end (
sessionInfo()).
-
Style and formatting guidelines:
- Use
message()for all output instead ofcat(), orprint(). - Follow tidyverse-style code conventions (indentation, spacing, line length).
- Add comments that explain why, not just what the code is doing.
- Use
-
Robustness enhancements:
- Check that any system binaries or CLI tools used in the script (via
system()orsystem2()) are available before running them.- Use
Sys.which()ornzchar(Sys.which("binary-name"))for these checks. - Exit gracefully with a message if a required binary is missing.
- Use
- Ensure any referenced files or paths exist before use (e.g., via
file.exists()). - If
optparseis used, include--verboseand--quietflags for output control if not already present.
- Check that any system binaries or CLI tools used in the script (via
📌 Template code:
<paste R code template here>📎 Code to refactor:
<paste your R code here>✍️ Metadata to include:
- Script name: <add the script name here or ask the LLM to
Come up with a suitable name> - Author:
- Created:
✅ Deliverable: Return only the reformatted R script, wrapped in a single code block, following all instructions above.