Commit 4090dcee authored by Lena Hersemann's avatar Lena Hersemann

added script version check: copies of the used script are automatically stored...

added script version check: copies of the used script are automatically stored in the results folder; re-running the script will check differences between the current and previous script and leaves the choice to the user of whether to overwrite existing data or not
parent c1c31c45
......@@ -17,6 +17,7 @@ suppressMessages(if (!require("docopt")) install.packages("docopt"))
suppressMessages(if (!require("knitr")) install.packages("knitr"))
suppressMessages(if (!require("stringr")) install.packages("stringr"))
suppressMessages(if (!require("rmarkdown")) install.packages("rmarkdown"))
suppressMessages(if (!require("diffobj")) install.packages("diffobj"))
# disabled because user script will fail if they load plyr before dplyr
#suppressMessages(if (!require("dplyr")) install.packages("dplyr"))
......@@ -64,6 +65,42 @@ if(!file.exists(r_script)){
}
# check previous script versions
script_name <- str_replace(basename(r_script), ".R", "")
current_script <- paste0(".", script_name, "_", format(Sys.time(), "%Y%m%d_%H%M.R"))
all_files <- file.info(list.files(".", pattern = paste0("\\.", script_name), all.files = T))
recent_script <- rownames(all_files)[which.max(all_files$mtime)]
if (identical(recent_script, character(0))) {
recent_script = r_script
}
row_diff <- system(paste("diff -U 0", recent_script, r_script, "| grep '^@' | wc -l"), intern = T) %>% as.numeric()
print(paste0("differences between the recent_script (", recent_script, ") and the currently used r_script (", r_script, ")"))
diffFile(recent_script, r_script, mode = "sidebyside")
if(row_diff > 0) {
print("ATTENTION: the script has been changed")
cat("Would you like to overwrite the existing data? [Yes/No]")
inp <- readLines(file("stdin"), n = 1L)
if (!inp == "Yes" & !inp %in% "No") {
stop("ATTENTION: incorrect input provided")
} else if (inp == "No") {
stop("ATTENTION: Script stopped based on your choice")
} else {
file.copy(r_script, current_script)
}
} else if (recent_script == r_script) {
file.copy(r_script, current_script)
} else {
file.copy(r_script, current_script)
file.remove(recent_script)
}
## Create a temporary script in the current working directory to ensure that created resources plots etc can be inlined
## To allow for additional resoucrces from the scripts directory to be inlined into the final documents, the directory
## of the script is exposed as a variable called RENDR_SCRIPT_DIR
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment