Search code examples
rrscript

Saving history for script run Rscript through terminal/console


for my work, I run scripts on virtual machines on a computer cluster. These jobs are typically large and have a big output. What I'd like to do is to run a script via the terminal. In the end, the script creates a duplicate of itself so that it contains every line that was part of the script (minus the last if necessary). This is quite vital for replicability and debugging in my work-life because I sometimes can't see which parameters or variables a particular job included as I submit the same script repeatedly just with slightly different parameters and the folders can't be version controlled.

Imagine this file test.R:

a <- rnorm(100)


#test

# Saving history for reproducibility to folder
savehistory(file = 'test2.R')

Now, I run this via the console on my virtual node and get the following error:

[XX home]$ Rscript test.R 
Error in.External2(C_savehistory, file): no history available to save
Calls: save history
Execution halted

Is there any command like save history that works inside a script that is just executed?

The desired outcome is a file called test2.R is saved that contains this:

a <- rnorm(100)


#test

# Saving history for reproducibility to folder

Solution

  • You could copy the file instead. Since you're using Rscript, the script name is included in commandArgs() in the form --file=test.R. A simple function like this will return the path of the executing script:

    get_filename <- function() {
      c_args <- commandArgs()
      r_file <- c_args[grepl("\\.R$", c_args, ignore.case = TRUE)]
      r_file <- gsub("--file=", "", r_file)
      r_file <- normalizePath(r_file)
      return(r_file)
    }
    

    Then you can copy the file as you see fit. For example, appending ".backup":

    script_name <- get_filename()
    backup_name <- paste0(script_name, ".backup")
    file.copy(script_name, backup_name)