Search code examples
rwarningsrcurlhttr

R suppressWarnings() not working with httr / curl


I'm trying to suppress a warning in R.

suppressWarnings(httr::GET("localhost:8080/does_not_exist"))

returns an error message

Error in curl::curl_fetch_memory(url, handle = handle) : 
  Failed to connect to localhost port 8080: Connection refused

I have also tried

suppressMessages(httr::GET("localhost:8080/does_not_exist"))
invisible(capture.output(httr::GET("localhost:8080/does_not_exist")))

but I still get the same error message.


Solution

  • I think you are trying to suppress an error rather than a warning in here. If you really don't want to see an error, you could wrap your function into try() and set the silent argument to TRUE. Something like this:

    try("a" + 1, silent = T) #returns nothing
    

    However, generally speaking I would advice not to do something like this, because error messages are useful rather than redundant.