# note use of map (not map_dbl), safely returns a lisstout <-map(x, safely(sum))str(transpose(out))
#> List of 2
#> $ result:List of 4
#> ..$ : num 1.39
#> ..$ : num 1.27
#> ..$ : num 2.17
#> ..$ : NULL
#> $ error :List of 4
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ :List of 2
#> .. ..$ message: chr "invalid 'type' (character) of argument"
#> .. ..$ call : language .Primitive("sum")(..., na.rm = na.rm)
#> .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
Other purrr function operators
purrr comes with three other function operators in a similar vein:
possibly(): returns a default value when there’s an error. It provides no way to tell if an error occured or not, so it’s best reserved for cases when there’s some obvious sentinel value (like NA).
quietly(): turns output, messages, and warning side-effects into output, message, and warning components of the output.
auto_browser(): automatically executes browser() inside the function when there’s an error.
The real work is done in capture_error which is defined in the package namespace. We can access it with the ::: operator. (Could also grab it from the function’s environment.)
urls <-c("adv-r"="https://adv-r.hadley.nz", "r4ds"="http://r4ds.had.co.nz/"# and many many more)path <-paste(tempdir(), names(urls), ".html")walk2(urls, path, download.file, quiet =TRUE)
Here we make a function operator that add a little delay in reading each page: