Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to produce a log-file which keeps track of all commands (stdin), results (stdout) and errors/warnings/messages (stderr) in the R console.

I am aware that there are a lot of logging-packages and I tried several like TeachingDemos (seems to ignore stderr completely) or R2HTML (seems to ignore messages), however, none of them seems to include everything from stderr.

Only knitr and markdown seem to be able to include everything into a single file. But using this workaround, I have to write R-scripts and I cannot freely write commands in the console.
Furthermore, I cannot include the knitr or markdown command in the same R-script (which is of course a minor 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()
like image 374
non-numeric_argument Avatar asked Jan 23 '14 11:01

non-numeric_argument


People also ask

Does R have a log file?

The logr package helps create log files for R scripts. The package provides easy logging, without the complexity of other logging systems. It is designed for analysts who simply want a written log of their program execution. The package is implemented as a wrapper to the base R sink() function.

How do I export output from R console?

You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.

What will be the output of log when executed on R console?

1 Answer. Explanation: Executing the above on R console or terminal will display a warning sign that NaN (Not a Number) will be produced in R console because it is not possible to take a log of a negative number(-).


2 Answers

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

R | tee mydir/mylog.txt

On windows, you can run the script in

R CMD BATCH yourscript.R

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

like image 107
flodel Avatar answered Oct 07 '22 00:10

flodel


On unices, I've often used the following code idiom with bash:

Some Command 2>&1 | tee NameOfOutputFile.txt

The "2>&1" says to take stderr and redirect it to stdout, which then gets piped to "tee". I will be experimenting with this and other ways of logging an R session.

Another unix trick is the "script" command, which starts a subshell whose I/O (basically everything you type and see in return) is logged to the specified file. And then exit the shell to end the script. Again, subject to experimentation. Since "sink" is native to R, I'll be trying that first.

By the way, I picked these tricks up using solaris, but they work the same running Cygwin, the unix emulator on Windows. Long time ago, I found that my Cygwin images were more up-to-date than the institutional installations of Solaris because the administrators had much more responsibility than just keeping the Solaris up-to-date. Mind you, the institutional machines were more powerful, so even though Cygwin was way more convenient, my personal machine simply didn't fill the need.

AFTERNOTE:

I took example code from page 99 of Shumway's Time Series Analysis and Its Applications With R examples. Here are the contents of a 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 invoked it using:

script < palette.R

It captures the commands from palette.R and the corresponding output. So, seems usable for batch mode. For interactive mode, I'm going to go with my original plan and use sink.

like image 40
user36800 Avatar answered Oct 07 '22 00:10

user36800