Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print warning messages as they occur

Tags:

r

warnings

I have the following code:

urls <- c(
    "xxxxx",
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz"        
)
readUrl <- function(url) {
out <- tryCatch(
    readLines(con=url, warn=FALSE),
    error=function(e) {
        message(paste("URL does not seem to exist:", url))
        message(e)
        return(NA)
    },
    finally=message(paste("Processed URL:", url))
)    
return(out)
}
y <- lapply(urls, readUrl)

When I run it, I get:

URL does not seem to exist: xxxxx  
cannot open the connectionProcessed URL: xxxxx    
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html  
Processed URL: http://en.wikipedia.org/wiki/Xz  
Warning message:  
In file(con, "r") : cannot open file 'xxxxx': No such file or directory  

but I expected:

URL does not seem to exist: xxxxx   
cannot open the connectionProcessed URL: xxxxx    
Warning message:    
In file(con, "r") : cannot open file 'xxxxx': No such file or directory  
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html  
Processed URL: http://en.wikipedia.org/wiki/Xz  

So, why do I get:

Warning message:    
In file(con, "r") : cannot open file 'xxxxx': No such file or directory   
like image 423
Dd Pp Avatar asked Aug 31 '12 08:08

Dd Pp


2 Answers

The call to readLines issues warnings. You can suppress warnings with suppressWarnings(). Try this:

readUrl <- function(url) {
  out <- tryCatch(
    suppressWarnings(readLines(con=url, warn=FALSE)),
    error=function(e) {
      message(paste("URL does not seem to exist:", url))
      message(e)
      return(NA)
    },
    finally=message(paste("Processed URL:", url))
  )    
  return(out)
}
y <- lapply(urls, readUrl)

The screen output:

URL does not seem to exist: xxxxx
cannot open the connectionProcessed URL: xxxxx
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Processed URL: http://en.wikipedia.org/wiki/Xz

Alternatively, you can use options(warn=1) to display warnings as they occur. Try this:

readUrl <- function(url) {
  op <- options("warn")
  on.exit(options(op))
  options(warn=1)
  out <- tryCatch(
    readLines(con=url, warn=FALSE),
    error=function(e) {
      message(paste("URL does not seem to exist:", url))
      message(e)
      return(NA)
    },
    finally=message(paste("Processed URL:", url))
  )    
  return(out)
}
y <- lapply(urls, readUrl)

The output:

Warning in file(con, "r") :
  cannot open file 'xxxxx': No such file or directory
URL does not seem to exist: xxxxx
cannot open the connectionProcessed URL: xxxxx
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Processed URL: http://en.wikipedia.org/wiki/Xz
like image 44
Andrie Avatar answered Nov 28 '22 12:11

Andrie


To display all warnings, simply run this:

options(warn=1)

Here's how it works:

  • Set warn = -1 (or any negative integer), to ignore all warnings
  • Set warn = 0 (the default), to display 10 or fewer warnings
  • Set warn = 1 (as above), to display all warnings
  • Set warn = 2 (or any positive integer greater than 2) to convert all warnings into errors

Further explanation available by running ?options:

integer value to set 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, coercible to integer), all warnings are turned into errors.

like image 95
stevec Avatar answered Nov 28 '22 10:11

stevec