Lohengrin
Lohengrin

Reputation: 125

Create warning log file when running R script

I'd like to create warning/error log for the R script.
Please see below example:

setwd(tempdir())
zz <- file("all.Rout", open="wt")
    sink(zz, type="message")
    for (i in 1:30){
           log(i-50)
         }
    sink(type="message")
    close(zz)

I was expecting that it will enlist all warnings:
Warning messages:
1: In log(i - 50) : NaNs produced
2: In log(i - 50) : NaNs produced
3: In log(i - 50) : NaNs produced

However for the loop i in 1:30 there is only one line in the all.rout file:
There were 30 warnings (use warnings() to see them)

Any idea how to fix it?

I have created the code based on another topic:

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

Upvotes: 0

Views: 1808

Answers (1)

Neal Fultz
Neal Fultz

Reputation: 9686

Try options(warn=1)

From ?options:

'warn': sets the handling of warning messages.  If 'warn' is
      negative all warnings are ignored.  If 'warn' is zero (the
      default) warnings are stored until the top-level function
      returns.  If 10 or fewer warnings were signalled they will be
      printed otherwise a message saying how many were signalled.
      An object called 'last.warning' is created and can be printed
      through the function 'warnings'.  If 'warn' is one, warnings
      are printed as they occur.  If 'warn' is two or larger all
      warnings are turned into errors.

Upvotes: 2

Related Questions