Intermediate Functional Programming with purrr
Colin Fay
Data Scientist & R Hacker at ThinkR
`possibly() creates a function that returns either:
the result
the value of otherwise
library(purrr) possible_sum <- possibly(sum, otherwise = "nop") possible_sum(1)
possible_sum("a")
0
"nop"
possibly()
can return:
ps <- possibly(sum, FALSE)
ps("a")
FALSE
NA
ps <- possibly(sum, NA)
ps("a")
NA
ps <- possibly(sum, "nope")
ps("a")
"nope"
ps <- possibly(sum, 0)
ps("a")
0
Intermediate Functional Programming with purrr