Rscript

Before we can submit jobs to the cluster, we first need to go over how to run jobs from the command line on our own local machine. Let’s say we have the following commands in a file called sim.R:

N <- 10000
p <- numeric(N)
n <- 10
sd_ratio <- 3
for (i in 1:N) {
  x <- rnorm(n)
  y <- rnorm(n, sd = sd_ratio)
  p[i] <- t.test(x, y, var.equal = TRUE)$p.value
}
results <- data.frame(
  method = "Student",
  n = n,
  sd_ratio = sd_ratio,
  p = p
)
R.version.string

# Save results
args <- commandArgs(trailingOnly = TRUE)
outfile <- if (length(args) >= 1) {
  args[1]
} else {
  hash <- sample(c(letters, 0:9), 8, replace = TRUE) |>
    paste(collapse = "")
  paste0("sim-", hash, ".rds")
}
saveRDS(results, outfile)

This generates data from two normal distributions, one of which has a variance 9 times larger than the other, then carries out a t-test in which we assume equal variance. This process is repeated 10,000 times.

The line where we print R.version.string isn’t important to the functioning of the simulation, but it will be an educational message for us to track during this tutorial.

The block at the end handles where to save the output, and allows two options which are described below.

Running an R script

We can run this from the command line using

Rscript sim.R

This saves the output in a file with a random name, like sim-a3f9bx2k.rds. Alternatively, you can include the filename you want by running the script like this:

Rscript sim.R my-results.rds

and then the results will be saved as my-results.rds. We’ll be using this sim.R script throughout the tutorial, and sometimes it will be useful to specify the name of the results, other times easier to leave them as random.

Combining results

If you run the above script a few times, you’ll end up with several sim-*.rds files. You can combine them with this script (which also deletes the intermediate results):

args <- commandArgs(trailingOnly = TRUE)
if (length(args) >= 1) {
  outfile <- args[1]
  files <- args[-1]
} else {
  outfile <- "results.rds"
  files <- list.files(pattern = "^sim-.*\\.rds$")
}
results <- do.call(rbind, lapply(files, readRDS))
saveRDS(results, outfile)
invisible(file.remove(files))

Running

Rscript combine.R

will combine all the sim-*.rds results, stack them, and save the output as results.rds. Alternatively, you can specify what the output file should be called and exactly what files should be combined with

Rscript combine.R my-results.rds file1.rds file2.rds