Skip to content

Instantly share code, notes, and snippets.

@russHyde
Last active May 19, 2020 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russHyde/9d91fcb213973b0ace5424b0520c82b7 to your computer and use it in GitHub Desktop.
Save russHyde/9d91fcb213973b0ace5424b0520c82b7 to your computer and use it in GitHub Desktop.
R script template using optparse
#!/usr/bin/env Rscript
###############################################################################
# Package names for non-test dependencies
pkgs <- c(
# CRAN
"magrittr",
"optparse",
# Bioconductor
# Local
NULL
)
for (pkg in pkgs) {
suppressPackageStartupMessages(
library(pkg, character.only = TRUE)
)
}
###############################################################################
define_parser <- function() {
description <- paste(
"Run my awesome workflow on some data."
)
parser <- OptionParser(
description = description
) %>%
add_option(
c("--input", "-i"), type = "character",
help = paste(
"Some filename."
)
) %>%
add_option(
"--threshold", type = "double",
help = paste(
"A numeric option."
)
) %>%
add_option(
"--filter_flag", action = "store_true", default = FALSE,
help = paste(
"Should the filter-flag be applied? Default: FALSE (no filters)"
)
) %>%
add_option(
c("--output", "-o"), type = "character", default = "output.tsv",
help = paste(
"Another filename, but with a default value."
)
) %>%
add_option(
"--test", action = "store_true", default = FALSE,
help = "Run any script-embedded tests then exit."
)
}
###############################################################################
run_my_awesome_workflow <- function(x) {
# Standard convoluted analysis scheme:
x
}
###############################################################################
{--script_name--} <- function(
input_file, threshold, filter_flag, output_file
) {
# Input
some_data <- scan(input_file, what = "character")
# Validate the input
stopifnot(...blah...(some_data))
# Small number of high-level function calls
my_results <- run_my_awesome_workflow(some_data)
# Output
write(my_results, output_file)
}
###############################################################################
main <- function(opt) {
# `main` should only parse the command line arguments (eg, split a
# `--drop_groups A,C` comma-separated string of values; extract some
# config values from a file), then call the `script_name` function
{--script_name--}(
input_file = opt[["input"]],
threshold = opt[["threshold"]],
filter_flag = opt[["filter_flag"]],
output_file = opt[["output"]]
)
}
###############################################################################
test <- function() {
# You can add some tests within the script if you want
stopifnot(requireNamespace("testthat"))
testthat::expect_equal(run_my_awesome_workflow(1), 1)
# remove me
testthat::expect_equal(
1, 2, info = "a failing test to test the tests that test your code"
)
}
###############################################################################
opt <- optparse::parse_args(define_parser())
# User can either run the tests or the main function
if (opt[["test"]]) {
test()
} else {
main(opt)
}
###############################################################################
@russHyde
Copy link
Author

#!/usr/bin/env bash                                                                                                               
                                                                                                                                  
# download the master version of the rscript template                                                                             
curl \                                                                                                                            
  https://gist.githubusercontent.com/russHyde/9d91fcb213973b0ace5424b0520c82b7/raw/rscript_template.R

@russHyde
Copy link
Author

russHyde commented Jan 16, 2020

Put the bash script into

<somewhere_on_PATH>/rscript_template

and chmod u+x it. Then use

rscript_template > my_new_script.R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment