How to purrr safely()

Foundations of Functional Programming with purrr

Auriel Fournier

Instructor

safely()

a <- list("unknown", 10) %>%
  map(safely(function(x) 
  x * 10, 
  otherwise = NA_real_))
[[1]]
[[1]]$result
[1] NA
[[1]]$error
<simpleError in x * 10: non-numeric 
argument to binary operator>

[[2]]
[[2]]$result
[1] 100
[[2]]$error
NULL
Foundations of Functional Programming with purrr

Reordering

a <- list("unknown",10) %>% 
 map(safely(function(x) 
             x * 10, 
        otherwise = NA_real_)) %>%
 transpose() 
$result
$result[[1]]
[1] NA
$result[[2]]
[1] 100


$error
$error[[1]]
<simpleError in x * 10: 
non-numeric argument to 
binary operator>
$error[[2]]
NULL
...
Foundations of Functional Programming with purrr

Let's purrr-actice!

Foundations of Functional Programming with purrr

Preparing Video For Download...