[R] session logging

Jacob Wegelin jwegelin at vcu.edu
Mon Aug 3 20:43:47 CEST 2009

Consider all the text that one sees on the console during an R session.

Is there a way, within R, to make all this text--both the "output" and the "messages"--automatically get copied to a single text file, in addition to seeing it on the console?

If I remember to save the console to a file at the end of my R session, that does it. But

(1) That requires pointing and clicking--can it be automated as a text command?

(2) It would be nice to issue the text command at the start of the R session, such as "log this entire session in mylog.txt, append", if this would ensure that the session is logged whether I remember to save the console or not.

As far as I can tell,


will hide the output from me and put it into mylog.txt. But it still shows me the error messages.

An attempt to put the output and messages into separate files returns an error:

> sink("junkout.txt", type="output")
> sink("junkmsg.txt", type="message")
Error in sink("junkmsg.txt", type = "message") :
   'file' must be NULL or an already open connection

and at any rate I'd like both messages in the same file, just like on the console.

People who run R at the unix command line apparently use the unix command -script-. But I mean something that will work within R, platform-independent.

A 2003 post to R-help suggests savehistory(), but this does *not* save the console; I tried it just now. Another post from the same thread suggests using emacs. But that is not platform-independent.

The existence of the 2003 thread suggests that this issue comes up periodically. Was it a deliberate design decision not to make logs available, in contrast to the way logging works in Stata?

I use the Rgui on a MacBook Pro:

> sessionInfo()
R version 2.8.1 (2008-12-22) 


attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] lme4_0.999375-28   Matrix_0.999375-21 lattice_0.17-17

loaded via a namespace (and not attached):
[1] boot_1.2-34 grid_2.8.1

Thanks for any insights.

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
E-mail: jwegelin at vcu.edu 
URL: http://www.people.vcu.edu/~jwegelin

More information about the R-help mailing list