Writing R and Shell files

R files (.R)

## name of file is:
## simulation_nind_10_mean_5_var_4.R
mean_fn <- function(n.ind = 10, m = 5, var = 4){
  rnorm(n = n.ind, mean = m, sd = sqrt(var)) %>%
    mean() %>%
    return()
}

replicate(1000, mean_fn()) %>%
  tibble(values = .) %>%
  summarize(
    values_sd = sd(values), 
    values_mean = mean(values),
    q_95 = qnorm(0.975)
    ) %>%
  mutate(
    lower_ci = values_mean - q_95*values_sd,
    upper_ci = values_mean + q_95*values_sd
    ) #%>%
## # A tibble: 1 × 5
##   values_sd values_mean  q_95 lower_ci upper_ci
##       <dbl>       <dbl> <dbl>    <dbl>    <dbl>
## 1     0.621        5.03  1.96     3.82     6.25
  #write.csv(
  #  paste0("nind_", n.ind, "_mean_", mean, "_var_", var, ".csv")
  #)

Shell files (.sh)

Name of file is script.sh

#!/bin/bash
#SBATCH –time 30:00
#SBATCH –mem 68G

module purge
module load R/3.5.1

Rscript simulation_nind_10_mean_5_var_4.R

Generating shell and R files “automatically”

Initially, I had code “similar” to above (this code would take a really long time to run, so having an option for a loop to run through different parameters in one file wasn’t an option). I changed the parameters n.ind, m and var and made a new R file for each combination of parameters. However, when I found a mistake in my code I would have to copy and paste my code from the fixed R file to all the other files… have you had this issue before?

  • Can apply a loop to alter code to change parameters…
n.ind <- c(
  rep(10, 3), 
  rep(100, 3), 
  rep(1000, 3)
  )
mean <- rep(c(1, 2, 5), 3)
var <- c(
  rep(1, 3),
  rep(5, 3), 
  rep(10, 3)
)

tibble(n.ind, mean, var)
## # A tibble: 9 × 3
##   n.ind  mean   var
##   <dbl> <dbl> <dbl>
## 1    10     1     1
## 2    10     2     1
## 3    10     5     1
## 4   100     1     5
## 5   100     2     5
## 6   100     5     5
## 7  1000     1    10
## 8  1000     2    10
## 9  1000     5    10
R.template <- "simulation_nind_10_mean_5_var_4.R" %>%
  readLines()

script.template <- "script.sh" %>%
  readLines()

script_num <- 1

for (i in n.ind){
  for (j in mean){
    for (k in sd){
      new.file.R <- paste0(
        "simulation_nind_", 
        i,
        "_mean_", 
        j,
        "_var_",
        k, 
        ".R"
      )
      
      new.file.sh <- paste0("script", script_num, ".sh")
        
      R.template %>%
        gsub("n.ind = 10", paste0("n.ind = ", i), .) %>%
        gsub("mean = 5", paste0("mean = ", j), .) %>%
        gsub("var = 4", paste0("var = ", k), .) %>%
        writeLines(con = new.file.R)
      
      script.template %>%
        gsub(
          "simulation_nind_10_mean_5_var_4.R", 
          new.file.R, 
          .) %>%
        writeLines(con = new.file.sh)
      
      script_num <- script_num + 1
    } 
  }
}

On the HPC, you can run the above 9 script files using one command:

for file in script{1..9}.sh; do
sbatch “$file”
done