Another way to possibly() purrr

Foundations of Functional Programming with purrr

Auriel Fournier

Instructor

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

[[2]]
[[2]]$result
[1] NA
[[2]]$error
<simpleError in x * 10: non-numeric 
argument to binary operator>

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

possibly()

a <- list(-10, "unknown", 10) %>%
     map(possibly(function(x) 
         x * 10, 
         otherwise = NA_real_))
a
[[1]]
[1] -100

[[2]]
[1] NA

[[3]]
[1] 100
Foundations of Functional Programming with purrr

Let's purrr-actice!

Foundations of Functional Programming with purrr

Preparing Video For Download...