如何在R中编写尝试捕获我想写trycatch处理从网上下载错误的代码。url <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz")y <- mapply(readLines, con=url)这两个语句成功运行。下面,我创建一个不存在的网址:url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")url[1]不存在。如何编写trycatch循环(功能)以便:当URL错误时,输出将是:“web URL是错误的,无法获取”。当URL错误时,代码不会停止,而是继续下载到URL列表的末尾?
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
# Do something, or tell me why it failedmy_update_function <- function(x){ tryCatch( # This is what I want to do... { y = x * 2 return(y) }, # ... but if an error occurs, tell me what happened: error=function(error_message) { message("This is my custom message.") message("And below is the error message from R:") message(error_message) return(NA) } )}
warning=
error=
- 3 回答
- 0 关注
- 569 浏览
添加回答
举报
0/150
提交
取消