diff --git a/tools/rendr/rend.R b/tools/rendr/rend.R index 6ec143407951fde557197cc23cece3a879d9b0d4..32a00f75527401dbd26451299db62b8b2214bd0c 100755 --- a/tools/rendr/rend.R +++ b/tools/rendr/rend.R @@ -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