safely()を安全に使う

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
purrr で学ぶ関数型プログラミング入門

並び替え

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
...
purrr で学ぶ関数型プログラミング入門

練習してみましょう!

purrr で学ぶ関数型プログラミング入門

Preparing Video For Download...