1

I'm an instructor, and my students have expressed interest in a record of the code I run in class. Since this is often off-the-cuff, I would like to have an easy function I could run at the end of class which would save everything I had run in the session. I know savehistory() will save my entire history, but that is not what I'm looking for.

Similar to this question, except I believe I have a pedagogically sound reason for wanting the history, and I'm looking to limit what gets saved on a per-session basis, rather than on the basis of the number of lines.

Community
  • 1
  • 1
AmeliaMN
  • 787
  • 1
  • 8
  • 9
  • Do you prepare the code beforehand? If not, perhaps you could use jupyter notebook running R as one option (isn't the notebook saved automatically?) – sckott Feb 29 '16 at 21:24
  • I prepare most code beforehand, which is available to students electronically and on handouts. But, there is often stuff that comes to me as we are working ("what if we wanted to add an interaction term?" "lets crank through the arithmetic on that" etc) and my students would like a record. – AmeliaMN Feb 29 '16 at 21:36
  • Possible duplicate of [How do I log an R session to a file?](http://stackoverflow.com/questions/9636656/how-do-i-log-an-r-session-to-a-file) – alexwhitworth Mar 01 '16 at 00:29

2 Answers2

4

I think if you invoke R with --no-restore-history (so that history from previous sessions isn't appended to the record for this one) and add

.Last <- function() { 
    if(interactive()) 
        try(savehistory(paste0("~/.Rhistory_", sys.time())))
}

to your Rprofile, you should get self-contained, and timestamped history files every time R closes naturally.

The .Last function, when defined in the global environment, is called directly before normal close. See ?.Last

NB: this won't preserve your history in the case of a fatal error (crash) in R itself, though that shouldn't come up much in a teaching context.

NB2: the above code will have generated file names with spaces in them. Depending on your OS, this could range from no big deal to hairpulling nightmarescape. If it's a problem, wrap sys.time() with your favorite datetime formatting code, e.g. format(sys.time(), "<format string>") or something from lubridate (probably, I don't actually know as I don't use it myself).

Thomas
  • 43,637
  • 12
  • 109
  • 140
Gabe Becker
  • 366
  • 2
  • 3
2

In the development version of rite on GitHub (>= v0.3.6), you can use the sinkstart() function to dump all of your code, all of your results, or both to a little interactive Tcl/tk widget, from which you could then just copy or save the output.

To make it work you can do this:

devtools::install_github("leeper/rite")

library("rite")
sinkstart(print.eval = FALSE, prompt.echo = "", split = TRUE)
## any code here
sinkstop() # stop printing to the widget

That will look something like:

sink

You can dynamically change what is printed in the widget from the context (right-click) menu on the widget. You can also dynamically switch between sinkstart() and sinkstop() if you only want some code and/or results output to there.

Full Disclosure: This is a package I developed.

Thomas
  • 43,637
  • 12
  • 109
  • 140