Cómo escribir trycatch en R

342

Quiero escribir trycatchcódigo para lidiar con el error 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 declaraciones 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 trycatchbucle (función) para que:

  1. Cuando la URL es incorrecta, la salida será: "la URL web es incorrecta, no se puede obtener".
  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 URL.
Dd Pp
fuente

Respuestas:

625

Bueno, entonces: bienvenido al mundo R ;-)

Aqui 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

trata de atraparlo

tryCatchdevuelve el valor asociado a la ejecución a exprmenos que haya un error o una advertencia. En este caso, se pueden especificar valores de retorno específicos (ver return(NA)arriba) al proporcionar una función de controlador respectiva (ver argumentos errory warningen ?tryCatch). Estas pueden ser funciones que ya existen, pero también puede definirlas dentro tryCatch()(como hice anteriormente).

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

Como hemos especificado que NAdebería devolverse en caso de error, el tercer elemento en yes NA. Si hubiéramos elegido NULLpara ser el valor de retorno, la longitud de ysólo habría sido 2en lugar de 3como lapply()será simplemente "ignorar" los valores de retorno 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 volverán NULL(es decir, en caso de un error o una condición de advertencia).

Mensaje de advertencia "no deseado"

Como warn=FALSEno parece tener ningún efecto, una forma alternativa de suprimir la advertencia (que en este caso no es realmente interesante) es utilizar

suppressWarnings(readLines(con=url))

en vez de

readLines(con=url, warn=FALSE)

Expresiones múltiples

Tenga en cuenta que también puede colocar varias expresiones en la "parte de expresiones reales" (argumento exprde tryCatch()) si las coloca entre corchetes (tal como lo ilustré en la finallyparte).

Rappster
fuente
Dado que la primera cadena en sus pastefunciones termina con un espacio, ¿por qué no omite el espacio y el sep=""?
seancarmody
2
@seancarmody: verdadero ;-) Estoy tan acostumbrado a juntar cadenas más largas / complicadas donde tengo que controlar los espacios escribiéndolos.
Rappster
3
¡Deberías usar paste0para eso!
seancarmody
66
paste0() Está en la base. Internamente, ambos paste()y paste0()llamar do_pastea paste.c . La única diferencia es paste0()que no pasa un separgumento.
jthetzel
1
@JulienNavarre: recuerde que la "parte de prueba" siempre devuelve el último objeto (actualmente, readLines(con=url, warn=FALSE)que es lo que podría salir mal). Así que si usted quiere añadir un mensaje, usted tendría que almacena el valor real retun en una variable: out <- readLines(con=url, warn=FALSE)seguido por message("Everything worked")seguido por outel fin de hacer de este el último objeto que realmente se volvió
Rappster
69

R utiliza 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 pueden manejarse: 'advertencias' y 'errores'. Lo importante a entender al escribir cada bloque de código es el estado de ejecución y el alcance. @fuente

heretolearn
fuente
55
Reemplazar error-handler-codeconcat("web url is wrong, can't get")
seancarmody
2
dejaste fuera la captura de mensajes
rawr
52

tryCatchtiene una estructura de sintaxis ligeramente compleja. Sin embargo, una vez que comprendamos las 4 partes que constituyen una llamada completa de tryCatch como se muestra a continuación, se hace fácil recordar:

expr : [ Obligatorio ] Código (s) R a evaluar

error : [ Opcional ] ¿Qué debería ejecutarse si se produjo un error al evaluar los códigos en expr

advertencia : [ Opcional ] ¿Qué debería ejecutarse si se produce una advertencia al evaluar los códigos en expr

finalmente : [ Opcional ] Lo que debería ejecutarse justo antes de salir de la llamada tryCatch, independientemente de si expr se ejecutó correctamente, con un error o con una advertencia

tryCatch(
    expr = {
        # Your code...
        # goes here...
        # ...
    },
    error = function(e){ 
        # (Optional)
        # Do this if an error is caught...
    },
    warning = function(w){
        # (Optional)
        # Do this if an warning is caught...
    },
    finally = {
        # (Optional)
        # Do this at the end before quitting the tryCatch structure...
    }
)

Por lo tanto, un ejemplo de juguete para calcular el registro de un valor podría verse así:

log_calculator <- function(x){
    tryCatch(
        expr = {
            message(log(x))
            message("Successfully executed the log(x) call.")
        },
        error = function(e){
            message('Caught an error!')
            print(e)
        },
        warning = function(w){
            message('Caught an warning!')
            print(w)
        },
        finally = {
            message('All done, quitting.')
        }
    )    
}

Ahora, ejecutando tres casos:

Un caso válido

log_calculator(10)
# 2.30258509299405
# Successfully executed the log(x) call.
# All done, quitting.

Un caso de "advertencia"

log_calculator(-10)
# Caught an warning!
# <simpleWarning in log(x): NaNs produced>
# All done, quitting.

Un caso de "error"

log_calculator("log_me")
# Caught an error!
# <simpleError in log(x): non-numeric argument to mathematical function>
# All done, quitting.

He escrito sobre algunos casos de uso útiles que uso regularmente. Encuentre más detalles aquí: https://rsangole.netlify.com/post/try-catch/

Espero que esto sea útil.

Rahul
fuente
34

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("This is my custom message.")
            message("And below is the error message from R:")
            message(error_message)
            return(NA)
        }
    )
}

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

Pablo
fuente
1
¿Debería haber llaves alrededor de la exprpieza, ya que hay dos líneas en lugar de una?
Paul
¡Gracias! Después de una doble verificación, no veo ninguna necesidad de llaves
Paul
Gracias por comprobarlo dos veces. Cuando ejecuto su código, obtuve Error: unexpected ')' in " )"y Error: unexpected ')' in " )". Agregar un par de llaves se resuelve el problema.
Paul
Para la mayoría de los casos de uso, tienes razón, ¡gracias! Ha sido arreglado.
Paul
23

Como acabo de perder dos días de mi vida tratando de resolver tryCatch para una función irr, 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 se obtuvieron errores en algunos casos en un gran conjunto de datos.

  1. Configure 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, en realidad necesita crear una función. Originalmente, para la parte de error, acabo de escribir error = return(NULL)y TODOS los valores volvieron nulos.

  3. Recuerde crear una salida secundaria (como mi "out") y para return(out).

James
fuente
3
¿Por qué es necesario el número 3?
Jan-glx