PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

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

 April 27, 2022     logging, r, stderr, warnings     No comments   

Issue

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()

Solution

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



Answered By - flodel
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing