Output error/warning log (txt file) when running R

2019-01-09 03:19发布

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?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-09 04:08

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"
查看更多
叛逆
3楼-- · 2019-01-09 04:23

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

(I don't have enough reputation to use the add comment function)

查看更多
登录 后发表回答