Safely
safely()
is an adverb. It takes a function (a verb) and returns a modified version. In this case, the modified function will never throw an error. Instead it always returns a list with two elements.
result
is the original result. If there is an error this will be NULLerror
is an error object. If the operation was successful the “error
” will be NULL.
A <- list(1, 10, "a")
map(.x = A, .f = safely(log))
#> [[1]]
#> [[1]]$result
#> [1] 0
#>
#> [[1]]$error
#> NULL
#>
#>
#> [[2]]
#> [[2]]$result
#> [1] 2.302585
#>
#> [[2]]$error
#> NULL
#>
#>
#> [[3]]
#> [[3]]$result
#> NULL
#>
#> [[3]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>