Skip to content

Commit

Permalink
Add support for R_STARTUP_RDATA={default,rename,remove,prompt} [#42]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Dec 4, 2019
1 parent 6b4b09a commit c8f7c66
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
10 changes: 10 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ Package: startup

Version: 0.13.0-9000 [2019-12-03]

NEW FEATURES:

* Setting environment variable 'R_STARTUP_RDATA' to "remove" will cause an
existing './.RData' file to be skipped by automatically removing it.
If "rename", it will be renamed to './.RData-YYYYMMDD_hhmmss' where the
timestamp is the last time the file was modified. If "prompt", then the
user is prompted whether they want to load the file or rename it. Thus,
setting 'R_STARTUP_RDATA=rename' in an .Renviron file will make sure no
.RData file is ever loaded while still preserving them.

BUG FIXES:

* startup(all = TRUE) would process startup folders in the current working
Expand Down
78 changes: 70 additions & 8 deletions R/startup.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ startup <- function(sibling = FALSE, all = FALSE,

debug(debug)

cmd_args <- getOption("startup.debug.commandArgs", commandArgs())

debug <- debug()
if (debug) {
cmd_args <- getOption("startup.debug.commandArgs", commandArgs())
r_home <- R.home()
r_arch <- .Platform$r_arch
r_os <- .Platform$OS.type
Expand Down Expand Up @@ -256,24 +257,85 @@ startup <- function(sibling = FALSE, all = FALSE,
logf("- Search path: %s", paste(sQuote(search()), collapse = ", "))
logf("- Loaded namespaces: %s",
paste(sQuote(loadedNamespaces()), collapse = ", "))

interactive <- interactive()

logf("startup::startup()-specific processing ... done")
logf("The following will be processed next by R:")
}

no_restore_data <- any(c("--no-restore-data", "--no-restore", "--vanilla") %in% cmd_args)
loads_RData <- has_RData <- FALSE
if (!no_restore_data) {
has_RData <- is_file(f <- "./.RData")
if (has_RData) {
f_info <- file_info(f, type = "binary")
env <- Sys.getenv("R_STARTUP_RDATA", "")
if (env == "") {
env <- "default"
} else if (debug) {
logf("- R_STARTUP_RDATA=%s", env)
}

if (env == "prompt") {
logf("- Prompting user whether they want to load %s or not", f_info)

no_restore_data <- any(c("--no-restore-data", "--no-restore", "--vanilla") %in% cmd_args)
loads_RData <- FALSE
prompt <- sprintf("Detected %s - do you want to load it? If not, it will be renamed. [Y/n]: ", f_info)
res <- TRUE
repeat({
ans <- readline(prompt)
ans <- gsub("(^[[:space:]]*|[[:space:]]*$)", "", ans)
ans <- tolower(ans)
if (ans %in% c("", "y", "yes")) {
res <- TRUE
break
} else if (ans %in% c("n", "no")) {
res <- FALSE
break
}
})
logf("- User wants to load it: %s", res)
env <- if (res) "default" else "rename"
}

if (env == "remove") {
logf("- Skipping %s by removing it", f_info)
file.remove(f)
has_RData <- is_file(f)
if (!has_RData) {
warning(sprintf("Skipped %s because R_STARTUP_RDATA=%s caused it to be removed", f, env), call. = FALSE)
}
} else if (env == "rename") {
fi <- file.info(f)
when <- fi[c("mtime", "ctime")]
keep <- vapply(when, FUN = inherits, "POSIXct", FUN.VALUE=FALSE)
when <- when[keep]
when <- sort(when, decreasing = TRUE)
when <- format(when[[1]], format = "%Y%m%d_%H%M%S")
f_new <- sprintf("%s.%s", f, when)
file.rename(f, f_new)
f_new_info <- file_info(f_new, type = "binary")
logf("- Skipping %s by renaming it to %s", f, f_new_info)
has_RData <- is_file(f)
if (!has_RData) {
warning(sprintf("Skipped %s because R_STARTUP_RDATA=%s caused it to be renamed to %s", f, env, f_new_info), call. = FALSE)
}
} else if (env != "default") {
warning(sprintf("Ignoring unknown value (%s) of %s",
sQuote(env), sQuote("R_STARTUP_RDATA")),
call. = FALSE)
}
}
}

if (debug) {
if (!no_restore_data) {
if (is_file(f <- "./.RData")) {
if (has_RData) {
loads_RData <- TRUE
logf("- %s", file_info(f, type = "binary"))
}
}

logf("- R_HISTFILE: %s", sQuote(Sys.getenv("R_HISTFILE")))
no_restore_history <- any(c("--no-restore-history", "--no-restore", "--vanilla") %in% cmd_args)
if (!no_restore_history && interactive) {
if (!no_restore_history && interactive()) {
if (is_file(f <- Sys.getenv("R_HISTFILE", "./.Rhistory"))) {
logf("- %s", file_info(f, type = "txt"))
}
Expand Down

0 comments on commit c8f7c66

Please sign in to comment.