Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active August 26, 2023 10:37
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tjmahr/9d510426d15c06b7f11ec11c08070b9e to your computer and use it in GitHub Desktop.
I am becoming addicted to CLIs
#!/usr/bin/env Rscript
raw_args <- commandArgs(TRUE)
args <- raw_args
set_command <- function(new) args[1] <<- new
get_command <- function() args[1]
get_rest <- function() args[-1]
# Resolve help
no_args <- length(args) == 0
asked_for_help <- any(c("-h", "--help", "help") %in% args)
legal_commands <- c("help", "refresh_exe", "simulate", "debug",
"process_models", "compute_power")
bad_command <- !(get_command() %in% legal_commands)
if (no_args | asked_for_help | bad_command) set_command("help")
# "x,y,z" => c("x", "y", "z")
parse_comma_sep_arg <- function(string) {
unlist(strsplit(string, ",", fixed = FALSE))
}
help <- function(...) {
msg <- glue::glue(
"Usage: do_sim [command] [arguments]
Commands:
help display usage
debug print info about command line arguments
refresh_exe recreates do_sim executable
simulate run a simulation for a study
process_models extract results from model and archive file
compute_power load simulations and compute % of models
that obtain signif results
Arguments for simulate:
study \"study1\", \"study2\"
n_participants number of participants per group.
(accepts comma-separated values: 40,50,60)
n_trials number of trials per condition
(accepts comma-separated values: 12,15,18)
n_simulations number of datasets/models to generate
Arguments for process_models:
n only process the first n models
"
)
cat(msg)
}
debug <- function(...) {
info <- list(
raw_args = raw_args,
args = args,
`args[1]` = args[1],
`get_command()` = get_command(),
`get_rest()` = get_rest())
cat(str(info))
}
refresh_exe <- function(...) {
file.copy("./do_sim.R", "./do_sim", overwrite = TRUE)
}
simulate <- function(...) {
dots <- list(...)
name <- dots[[1]]
stopifnot(tolower(name) %in% c("study1", "study2"))
if (name == "study1") simulate1(...)
if (name == "study2") simulate2(...)
}
simulate1 <- function(...) {
dots <- list(...)
n <- as.numeric(parse_comma_sep_arg(dots[[2]]))
j <- as.numeric(parse_comma_sep_arg(dots[[3]]))
r <- as.numeric(dots[[4]])
stopifnot(!is.na(n), !is.na(j), !is.na(r))
suppressWarnings({source("./R/study1.R")})
simulate_study1(n, j, r)
}
simulate2 <- function(...) {
dots <- list(...)
n <- as.numeric(parse_comma_sep_arg(dots[[2]]))
j <- as.numeric(parse_comma_sep_arg(dots[[3]]))
r <- as.numeric(dots[[4]])
stopifnot(!is.na(n), !is.na(j), !is.na(r))
suppressWarnings({source("./R/study2.R")})
simulate_study2(n, j, r)
}
process_models <- function(n = NULL) {
suppressWarnings({source("./helpers.R")})
models <- list_models()
if (is.null(n)) n <- length(models)
n <- as.numeric(n)
if (is.na(n)) {
stop("Please specify a valid value for n", call. = FALSE)
}
models <- head(models, n)
for (model in models) process_model(model)
invisible(NULL)
}
compute_power <- function(...) {
# stub
}
invisible(rlang::invoke(get_command(), get_rest()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment