Cómo escribir trycatch en R


Quiero escribir trycatch código para hacer frente a los errores en la descarga desde la web.

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)

Estas dos sentencias se ejecutan con éxito. A continuación, creo una dirección web inexistente:

url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")

url[1] no existe. ¿Cómo se escribe un bucle trycatch (función) para que:

  1. Cuando la URL es incorrecta, la salida será: "web URL is wrong, can't get".
  2. Cuando la URL es incorrecta, el código no se detiene, sino que continúa descargándose hasta el final de la lista de ¿URLs?
Author: smci, 2012-08-30

4 answers

Bien entonces: bienvenido al mundo R; -)

Aquí tienes

Configurando el código

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's 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's 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)
}

Aplicando el código

> 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's 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

Investigando la salida

> 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

Observaciones adicionales

TryCatch

tryCatch devuelve el valor asociado a la ejecución expr a menos que haya un error o una advertencia. En este caso, se pueden especificar valores de retorno específicos (ver return(NA) arriba) suministrando una función de controlador respectiva (ver argumentos error y warning en ?tryCatch). Estas pueden ser funciones que ya existen, pero también puedes definirlas dentro de tryCatch() (como hice anteriormente).

Las implicaciones de elegir valores de retorno específicos de las funciones del controlador

Como hemos especificado que NA debe devolverse en caso de error, el tercer elemento en y es NA. Si hubiéramos elegido NULL como el valor de retorno, la longitud de y simplemente habría sido 2 en lugar de 3 como lapply() simplemente "ignore" devuelve valores que son NULL. También tenga en cuenta que si no especifica un valor de retorno explícito a través de return(), las funciones del controlador devolverán NULL (es decir, en caso de un error o una condición de advertencia).

"Mensaje de advertencia no deseado

Como warn=FALSE no parece tener ningún efecto, una forma alternativa de suprimir la advertencia (que en este caso no es realmente de interés) es usar

suppressWarnings(readLines(con=url))

En lugar de

readLines(con=url, warn=FALSE)

Múltiples expresiones

Tenga en cuenta que también puede colocar múltiples expresiones en la "parte de expresiones reales" (argumento expr de tryCatch()) si las envuelve entre corchetes (como ilustré en la parte finally).

 444
Author: Rappster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-11 17:12:21

R usa funciones para implementar el bloque try-catch:

La sintaxis se ve algo así:

result = tryCatch({
    expr
}, warning = function(warning_condition) {
    warning-handler-code
}, error = function(error_condition) {
    error-handler-code
}, finally={
    cleanup-code
})

En tryCatch() hay dos 'condiciones' que se pueden manejar: 'advertencias' y 'errores'. Lo importante a entender al escribir cada bloque de código es el estado de ejecución y el alcance. @ source

 52
Author: heretolearn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-28 11:19:39

Aquí va un ejemplo sencillo:

# Do something, or tell me why it failed
my_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("My message is here!")
            message("And below is the error message from R:")
            message(error_message)
            return(NA)
        }
    )
}

Si también desea capturar una "advertencia", simplemente agregue warning= similar a la parte error=.

 18
Author: Paul Paczuski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-05 21:57:27

Dado que acabo de perder dos días de mi vida tratando de resolver para tryCatch para una función tir, pensé que debería compartir mi sabiduría (y lo que falta). FYI-irr es una función real de FinCal en este caso donde consiguió errores en algunos casos en un conjunto de datos grande.

  1. Configura tryCatch como parte de una función. Por ejemplo:

    irr2 <- function (x) {
      out <- tryCatch(irr(x), error = function(e) NULL)
      return(out)
    }
    
  2. Para que el error (o advertencia) funcione, realmente necesita crear una función. Originalmente para la parte de error solo escribí error = return(NULL) y TODO los valores volvieron null.

  3. Recuerde crear una sub-salida (como mi "salida") y return(out).

 16
Author: James,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-26 10:47:08