R suppressWarnings() 不适用于 httr / curl

R suppressWarnings() not working with httr / curl

我正在尝试抑制 R 中的警告。

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

returns 一条错误消息

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

我也试过了

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

但我仍然收到相同的错误消息。

我认为您在此处试图抑制错误而不是警告。如果您真的不想看到错误,可以将您的函数包装到 try() 并将 silent 参数设置为 TRUE。像这样:

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

但是,一般来说我建议不要做这样的事情,因为错误信息是有用的而不是多余的。