HenrikBengtsson / startup

:wrench: R package: startup - Friendly R Startup Configuration
https://henrikbengtsson.github.io/startup/
163 stars 5 forks source link

R_STARTUP_RDATA=skip #82

Open HenrikBengtsson opened 4 years ago

HenrikBengtsson commented 4 years ago

It's possible to "hide" and then "unhide" a .RData during the R startup process by renaming it and then creating a .First() function to un-rename it. This would make it possible to also support R_STARTUP_RDATA=skip with a warning and a skip-silently to skip without a message.

Working prototype:

local({
  if (utils::file_test("-f", f <- ".RData")) {
    f2 <- paste(f, ".hide", sep="")
    file.rename(f, f2)
    message("Hiding .RData")
    assign(".First", function() {
      message("Unhiding .RData")
      file.rename(f2, f)
      rm(list = ".First", envir = globalenv())
    }, envir = globalenv())
  }
})

Care needs to be taken such that any existing .First() is not overridden.

HenrikBengtsson commented 4 years ago

With #83 implemented, the above will look like:

local({
  if (utils::file_test("-f", ".RData")) {
    fn_hide <- tempfile(".RData.", tmpdir = ".")
    if (file.rename(".RData", fn_hide)) {
      message("Hiding .RData")
      startup::on_post_startup({
        message("Unhiding .RData")
        file.rename(fn_hide, ".RData")
      })
    }
  }
})

The problem with R_STARTUP_RDATA=skip is that it cannot be reliably be implemented as an atomic transaction, e.g. if two R processes launch at the same, one process may "hide" the .RData file so that the first one does not see it, and then "unhide" so that the second R process will actually load it.