-
-
Save arthurgailes/7ee0fb6c560ad4370985d4afbf5774b2 to your computer and use it in GitHub Desktop.
comparing purrr to furrr
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
if(!require(pacman)) install.packages('pacman') | |
pacman::p_load(tigris, tidycensus, furrr, purrr, tictoc, ggplot2) | |
options(tigris_use_cache = FALSE) # make things equal between runs | |
state_names <- c(state.name, "District of Columbia") | |
names(state_names) <- state_names | |
# purrr: | |
tictoc::tic() | |
age_maps <- map(state_names, ~{ | |
age_data <- get_acs( | |
geography = "tract", | |
variables = "B01002_001", | |
state = .x, | |
geometry = TRUE | |
) | |
ggplot(age_data, aes(fill = estimate)) + | |
geom_sf(color = NA) + | |
theme_void() + | |
scale_fill_viridis_c(option = "magma") + | |
labs(title = .x, | |
subtitle = "Median age, 2017-2021 ACS", | |
fill = "Estimate") | |
}) | |
tictoc::toc() | |
tictoc::tic() | |
# instantiate parallel workers | |
plan(multisession, workers = min(4, availableCores())) # varies by machine | |
age_maps <- future_map(state_names, ~{ | |
age_data <- get_acs( | |
geography = "tract", | |
variables = "B01002_001", | |
state = .x, | |
geometry = TRUE | |
) | |
ggplot(age_data, aes(fill = estimate)) + | |
geom_sf(color = NA) + | |
theme_void() + | |
scale_fill_viridis_c(option = "magma") + | |
labs(title = .x, | |
subtitle = "Median age, 2017-2021 ACS", | |
fill = "Estimate") | |
}) | |
tictoc::toc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment