How to make an R-session log file that combines commands, results and warnings / messages / errors from the R-console

I would like to create a log file that tracks all commands (stdin), results (stdout) and errors / warnings / messages (stderr) in the R console.

I know there are many registration packages, and I tried several such as TeachingDemos (seems to completely ignore stderr) or R2HTML (seems to ignore messages), however none of them seem to include everything from stderr.

Only knitr and markdown seem to be able to include everything in one file. But using this workaround, I have to write R scripts, and I cannot freely write commands in the console.
In addition, I cannot include the knitr or markdown in the same R-script (which, of course, is a small problem).

Here is an example :

 library(TeachingDemos) library(R2HTML) library(TraMineR) logdir <- "mylog.dir" txtStart(file=paste(logdir,"test.txt", sep=""), commands=TRUE, results=TRUE, append=FALSE) HTMLStart(outdir = logdir, file = "test", echo=TRUE, HTMLframe=FALSE) ## Messages, warnings and errors message("Print this message.") warning("Beware.") "a" + 1 geterrmessage() ## Some example application with the TraMiner package ## which uses messages frequently data(mvad) mvad.seq <- seqdef(mvad[, 17:86]) mvad.ham <- seqdist(mvad.seq, method="HAM") txtStop() HTMLStop() 
+8
logging r warnings stderr
source share
2 answers

If you are using R from Unix / Linux / Mac / etc. terminal, you can do:

 R | tee mydir/mylog.txt 

In windows you can run the script in

 R CMD BATCH yourscript.R 

and your result will appear in the same folder as yourscript.out

+7
source share

In unices, I often used the following code id with bash:

 Some Command 2>&1 | tee NameOfOutputFile.txt 

"2> & 1" says to accept stderr and redirect it to stdout, after which it will be transmitted over the channel to "tee". I will experiment with this and other ways to register session R.

Another trick of unix is ​​the script command, which launches a subnet whose I / O (basically everything you enter and see in the response) is written to the specified file. Then exit the shell to finish the script. Again, subject to experimentation. Since the β€œshell” is native to R, I will try first.

By the way, I selected these tricks using solaris, but they work with the same running Cygwin, unix emulator on Windows. Once upon a time, I discovered that my Cygwin images were more modern than institutional Solaris installations, because administrators had a lot more responsibility than just updating Solaris. Keep in mind that institutional machines were more powerful, so although Cygwin was more convenient, my personal machine just didn't make up for it.

AFTERNOTE:

I took the sample code from page 99 Shumway Time Series Analysis and its Applications with R examples . Here are the contents of the test file palette. R:

 r plot(gnp) acf2(gnp, 50) gnpgr = diff(log(gnp)) # growth rate plot(gnpgr) acf2(gnpgr, 24) sarima(gnpgr, 1, 0, 0) # AR(1) sarima(gnpgr, 0, 0, 2) # MA(2) ARMAtoMA(ar=.35, ma=0, 10) # prints psi-weights quit("no") exit 

I called it using:

script <palette.R

It captures commands from the R. palette and the corresponding output. Thus, it can be used for batch mode. For interactive mode, I'm going to go with my original plan and use the sink.

+1
source share

All Articles