Saving history for script run Rscript through terminal/console

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)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top