如何在R中編寫嘗試捕獲我想寫trycatch處理從網(wǎng)上下載錯(cuò)誤的代碼。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)這兩個(gè)語句成功運(yùn)行。下面,我創(chuàng)建一個(gè)不存在的網(wǎng)址:url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")url[1]不存在。如何編寫trycatch循環(huán)(功能)以便:當(dāng)URL錯(cuò)誤時(shí),輸出將是:“web URL是錯(cuò)誤的,無法獲取”。當(dāng)URL錯(cuò)誤時(shí),代碼不會(huì)停止,而是繼續(xù)下載到URL列表的末尾?
3 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
result = tryCatch({ expr}, warning = function(warning_condition) { warning-handler-code}, error = function(error_condition) { error-handler-code}, finally={ cleanup-code})

慕的地6264312
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
# 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 關(guān)注
- 598 瀏覽
添加回答
舉報(bào)
0/150
提交
取消