避免 "Could not resolve host" 错误停止 R 中的程序 运行

Avoiding "Could not resolve host" error to stop the program running in R

我使用 R 中 Rcurl 包中的 getURL 函数从 link 列表中读取内容。

当试图获取列表中损坏的 link 时,我收到错误消息“函数错误(类型、消息、asError = TRUE):无法解析主机:" 并且程序停止 运行.

我使用 Try 命令试图避免程序停止,但它不起作用。

try(getURL(URL, ssl.verifypeer = FALSE, useragent = "R")

有关如何避免程序在尝试损坏 link 时停止 运行 的任何提示?

您需要进行某种类型的错误处理。我认为 tryCatch 实际上更适合你的情况。

我假设您在链接的循环中,然后您可以检查 try/tryCatch 的响应以查看是否抛出错误,如果是这样,请转到您的以下迭代使用 next.

循环
status <- tryCatch(
    getURL(URL, ssl.verifypeer=FALSE, useragent="R"),
    error = function(e) e
)

if(inherits(status,  "error")) next