Skip to content

Instantly share code, notes, and snippets.

@arthurgailes
Forked from walkerke/purrr_maps.R
Last active May 14, 2024 16:20
Show Gist options
  • Save arthurgailes/7ee0fb6c560ad4370985d4afbf5774b2 to your computer and use it in GitHub Desktop.
Save arthurgailes/7ee0fb6c560ad4370985d4afbf5774b2 to your computer and use it in GitHub Desktop.
comparing purrr to furrr
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