Set up command logger that persists through RStudio restart with Rprofile

I'm trying to set up a method to combine to processes: log all commands with a timestamp (matahari package) and restarting RStudio often to get a fresh state (h/t Jenny Bryan).

My current best method for logging commands is to use the .Rprofile and run dance_start() and dance_stop() within the .First and .Last functions respectively (with extra code in the .Last to write the table to a .csv). This works perfectly fine if I launches RStudio and then leave it running through the entire process, and then closesto finish the project.

The problem I'm facing is trying to get this log to persist through restart (either through Ctrl+Shift+F10 or Session>Restart). I've put some tests in and it seems to be the case that the .Last function doesn't get ran on a restart, only on a hard exit, and that the .First gets run each time. The result is that only the commands executed after the final restart get stored in the table.

My current workaround is to define a function my_restart() to be used in lieu of restarting through keyboard or menu. It runs the exact same code as the .Last (stop recording and write to file) followed by the console command to restart (.rs.restartR()). The problem with that is that the restart from the command line (using this function at least) doesn't do a full fresh session.

My assumption is that restarting RStudio is a different process than closing it. This makes sense becuase once RStudio is closed I wouldn't expect it to be able to re-open itself. Therefore, the restart isn't the same hard close that calls the .Last function.

Here's an example of a .Rprofile that does what I'm talking about with the partial restart.

my_restart <- function() {
  cat("\nGoodbye at ", date(), "\n")
  matahari::dance_stop()
  log <- matahari::dance_tbl()
  log[c("value", "path", "contents", "selection")] <- NULL
  log <- data.frame(lapply(log, as.character), stringsAsFactors = FALSE)
  path <- paste("Logs/log_", gsub(pattern = " ", x = as.character(Sys.Date()), replacement = "_"), ".csv", sep = "")
  path <- gsub(pattern = ":", replacement = "", x = path)

  write.table(log, 
              path,
              append = TRUE, 
              sep = ",",
              row.names = FALSE,
              col.names = FALSE)

  .rs.restartR()
}

.First <- function(){
  cat("\nWelcome at", date(), "\n")
  matahari::dance_start()
}

.Last <- function(){
  cat("\nGoodbye at ", date(), "\n")
  matahari::dance_stop()
  log <- matahari::dance_tbl()
  log[c("value", "path", "contents", "selection")] <- NULL
  log <- data.frame(lapply(log, as.character), stringsAsFactors = FALSE)
  path <- paste("Logs/log_", gsub(pattern = " ", x = as.character(Sys.Date()), replacement = "_"), ".csv", sep = "")
  path <- gsub(pattern = ":", replacement = "", x = path)
  write.table(log, 
              path,
              append = TRUE, 
              sep = ",",
              row.names = FALSE,
              col.names = FALSE)
}

I'm looking for a solution that allows me to restart keep a log of all commands entered with timestamps that persists through restarts. I might expect this to look like 1) an R function that does a full-stop restart of RStudio, 2) a different log creating package, or 3) some entirely different process entirely.

Thanks in advance!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.