Я хочу написать код trycatch
для 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
(функцию), чтобы:
Хорошо, тогда: добро пожаловать в мир R;-)
Здесь вы идете
urls <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz",
"xxxxx"
)
readUrl <- function(url) {
out <- tryCatch(
{
# Just to highlight: if you want to use more than one
# R expression in the "try" part then you'll have to
# use curly brackets.
# 'tryCatch()' will return the last evaluated expression
# in case the "try" part was completed successfully
message("This is the 'try' part")
readLines(con=url, warn=FALSE)
# The return value of `readLines()` is the actual value
# that will be returned in case there is no condition
# (e.g. warning or error).
# You don't need to state the return value via `return()` as code
# in the "try" part is not wrapped insided a function (unlike that
# for the condition handlers for warnings and error below)
},
error=function(cond) {
message(paste("URL does not seem to exist:", url))
message("Here the original error message:")
message(cond)
# Choose a return value in case of error
return(NA)
},
warning=function(cond) {
message(paste("URL caused a warning:", url))
message("Here the original warning message:")
message(cond)
# Choose a return value in case of warning
return(NULL)
},
finally={
# NOTE:
# Here goes everything that should be executed at the end,
# regardless of success or error.
# If you want more than one expression to be executed, then you
# need to wrap them in curly brackets ({...}); otherwise you could
# just have written 'finally=<expression>'
message(paste("Processed URL:", url))
message("Some other message at the end")
}
)
return(out)
}
> y <- lapply(urls, readUrl)
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
URL does not seem to exist: xxxxx
Here the original error message:
cannot open the connection
Processed URL: xxxxx
Some other message at the end
Warning message:
In file(con, "r") : cannot open file 'xxxxx': No such file or directory
> head(y[[1]])
[1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
[2] "<html><head><title>R: Functions to Manipulate Connections</title>"
[3] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
[4] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"
[5] "</head><body>"
[6] ""
> length(y)
[1] 3
> y[[3]]
[1] NA
tryCatch
tryCatch
возвращает значение, связанное с выполнением expr
, если нет ошибки или предупреждения. В этом случае конкретные возвращаемые значения (см. return(NA)
выше) могут быть заданы путем подачи соответствующей функции обработчика (см. Аргументы error
и warning
в ?tryCatch
). Это могут быть уже существующие функции, но вы также можете определить их в tryCatch()
(как я уже говорил выше).
Последствия выбора конкретных возвращаемых значений функций обработчика
Как мы уже указывали, NA
должен быть возвращен в случае ошибки, третий элемент в y
равен NA
. Если бы мы выбрали NULL
как возвращаемое значение, длина y
была бы только 2
вместо 3
, так как lapply()
будет просто игнорировать возвращаемые значения NULL
. Также обратите внимание, что если вы не укажете возвращаемое значение Явное через return()
, функции обработчика вернут NULL
(то есть в случае ошибки или предупреждения).
Предупреждение "Нежелательное"
Поскольку warn=FALSE
, похоже, не имеет никакого эффекта, альтернативный способ подавить предупреждение (что в данном случае не представляет особого интереса) заключается в использовании
suppressWarnings(readLines(con=url))
вместо
readLines(con=url, warn=FALSE)
Несколько выражений
Обратите внимание, что вы также можете поместить несколько выражений в "часть фактических выражений" (аргумент expr
of tryCatch()
), если вы оберните их фигурными скобками (как показано на части finally
).
R использует функции для реализации блока try-catch:
Синтаксис выглядит примерно так:
result = tryCatch({
expr
}, warning = function(warning_condition) {
warning-handler-code
}, error = function(error_condition) {
error-handler-code
}, finally={
cleanup-code
})
В tryCatch() есть два условия, которые могут быть обработаны: "предупреждения и ошибки". Важное значение для понимания при написании каждого блока кода - это состояние выполнения и область действия. @source
error-handler-code
cat("web url is wrong, can't get")
Вот простой пример:
# Do something, or tell me why it failed
my_update_function <- function(x){
tryCatch(
# This is what I want to do.
# If executing more than one expression you need curly braces.
{
y = x * 2
return(y)
},
# ... but if an error occurs, tell me what happened:
error=function(error_message) {
message("My message is here!")
message("And below is the error message from R:")
message(error_message)
return(NA)
}
)
}
Если вы также хотите перехватить "предупреждение", просто добавьте warning=
аналогично error=
.
expr
, так как вместо одной есть две строки?
Поскольку я просто потерял два дня своей жизни, пытаясь решить для tryCatch для irr-функции, я думал, что должен поделиться своей мудростью (и чего не хватает). FYI - irr - фактическая функция от FinCal, в этом случае, где в нескольких случаях были обнаружены ошибки в большом наборе данных.
Настройте tryCatch как часть функции. Например:
irr2 <- function (x) {
out <- tryCatch(irr(x), error = function(e) NULL)
return(out)
}
Чтобы ошибка (или предупреждение) работала, вам действительно нужно создать функцию. Я изначально для части ошибки просто написал error = return(NULL)
, а значения ALL вернулись к нулю.
Не забудьте создать суб-выход (например, мой "выход" ) и return(out)
.
Я написал простое введение в tryCatch
и некоторые примеры его использования здесь: https://rsangole.netlify.com/post/try-catch/
Надеюсь, это полезно.
paste
заканчивается пробелом, почему бы не опустить пробел иsep=""
?