Created
February 8, 2022 08:37
-
-
Save jvieroe/9747ec2b4c5bcd2c14c6c0d7d60b0690 to your computer and use it in GitHub Desktop.
intro to future_map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(future) | |
library(furrr) | |
library(gapminder) | |
# ------ create dummy dataset | |
df <- gapminder::gapminder | |
table(df$year) | |
# ----- split dataset into a list with elements based on the f variable | |
map_list <- df %>% | |
split(., f = .$year) | |
# ----- define a function to be applied to each list element | |
test_fun <- function(my_dat, output) { | |
output <- my_dat %>% | |
group_by(continent) %>% | |
summarise(life_exp = mean(lifeExp, na.rm = TRUE)) | |
} | |
# ----- define no. of cores to use, here I use all cores minus 3 | |
no_cores <- availableCores() - 3 | |
no_cores | |
# ----- start parallel processing session | |
future::plan(multisession, workers = no_cores) | |
# ----- apply function to each list element | |
output_list <- future_map(.x = map_list, | |
.f = test_fun, | |
.progress = TRUE) | |
# ----- stop parallel processing | |
future::plan(sequential) | |
# ----- unpack your data | |
output <- bind_rows(output_list, .id = "id") | |
output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment