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

Wednesday, April 27, 2022

[FIXED] How to print warning messages as they occur

 April 27, 2022     r, warnings     No comments   

Issue

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   

Solution

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


Answered By - Andrie
Answer Checked By - David Marino (PHPFixing Volunteer)
  • 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