1
2make_fake_profile  <- function(session_output) {
3  profile <- tempfile()
4
5  ## Include the real profile as well, if any
6  user <- Sys.getenv("R_PROFILE_USER", NA_character_)
7  local <- ".Rprofile"
8  home  <- path.expand("~/.Rprofile")
9  if (is.na(user) && file.exists(local)) user <- local
10  if (is.na(user) && file.exists(home)) user <- home
11  if (!is.na(user) && file.exists(user)) file.append(profile, user)
12
13  last <- substitute(
14    function() {
15      si <- tryCatch(sessioninfo::session_info(), error = identity)
16      l <- if (file.exists(`__output__`)) {
17        readRDS(`__output__`)
18      } else {
19        list()
20      }
21      saveRDS(c(l, list(si)), `__output__`)
22    },
23    list(`__output__` = session_output)
24  )
25
26  cat(".Last <-", deparse(last), sep = "\n", file = profile,
27      append = TRUE)
28
29  profile
30}
31
32get_session_info <- function(package, session_output) {
33
34  ## Extract session info for this package
35  session_info <- tryCatch(
36    suppressWarnings(readRDS(session_output)),
37    error = function(e) NULL
38  )
39
40  session_info <- Filter(
41    function(so) package %in% so$packages$package,
42    session_info
43  )
44
45  if (length(session_info) > 0) session_info[[1]] else NULL
46}
47