Skip to content

Instantly share code, notes, and snippets.

@ryanbthomas
Created February 11, 2020 14:50
Show Gist options
  • Save ryanbthomas/771e9bffebc653778e69e3e2c3c5bd51 to your computer and use it in GitHub Desktop.
Save ryanbthomas/771e9bffebc653778e69e3e2c3c5bd51 to your computer and use it in GitHub Desktop.
Iteration Safely over functions
# Rules
library(purrr)
library(dplyr)
library(tibble)
dat <- tibble(x = 1:3, y = 4:6)
f1 <- function(tbl) {
mutate(tbl, z = x*y) %>% select(z)
}
f2 <- function(tbl) {
stop("I don't like this function", call. = FALSE)
}
f3 <- function(tbl) {
mutate(tbl, z = x^2 + y^2) %>% select(z)
}
rules <- list(f1, f2, f3)
safe_rules <- purrr::map(rules, purrr::safely)
results <- purrr::map(safe_rules, .f = ~ .x(dat) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment