如何在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 關注
- 605 瀏覽
添加回答
舉報
0/150
提交
取消
