Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output error/warning log (txt file) when running R script under command line

If I run R script under command line (actually I run that from calling in VBA), how can I output any error/warning messages to a txt file?

like image 941
Joyce Avatar asked Jul 26 '12 09:07

Joyce


2 Answers

You can use sink() to divert messages as well as warnings to a file. The trick is to set the argument type="message":

Here is an example adapted from the help for ?sink:

setwd(tempdir())

## capture messages and errors to a file.
zz <- file("all.Rout", open="wt")
sink(zz, type="message")

try(log("a"))

## reset message sink and close the file connection
sink(type="message")
close(zz)

## Display the log file
readLines("all.Rout")
[1] "Error in log(\"a\") : Non-numeric argument to mathematical function"
like image 191
Andrie Avatar answered Sep 22 '22 20:09

Andrie


To close the connection with the log file you have to use sink(type="message") instead of sink() and then close(zz).

like image 31
André le Blond Avatar answered Sep 21 '22 20:09

André le Blond