Threads and Tidyverse

While it is true that the Census API and tidycensus only supports pulling one state at a time for anything smaller then a county, when you want to pull all Census Tracts or Block Groups in America you can run it through purrr::map.

library(tidyverse)
library(tidycensus)
library(sf)
library(purrr)

map(
  state.abb,
  \(x) get_decennial(
    geography = 'block group',
    variables = 'P1_001N',
    state = x,
    geometry = TRUE,
    cache_table = TRUE
  )
) %>%
  bind_rows() %>%
  write_sf('/tmp/blockgroup-population-of-usa.gpkg')

A better way to do this is to use furr::future_map and pull multiple states at once. You do have to be careful how many threads you pull at once to not exceed Census API limits but if you limit it to a few threads and sign up for a free Census API Key you should be okay. On Linux it’s possible to use ‘multicore’ for even slightly better performance, as uses a fork of the existing session rather then opening a separate R session as is necessary on Windows as forking isn’t supported in that operating system.

library(tidyverse)
library(tidycensus)
library(sf)
library(furrr)

plan('multisession', workers = 3)

future_map(state.abb,
           \(x) get_acs(
             'tract',
             'B19013_001',
             cache_table = TRUE,
             year = 2023,
             state = x,
             geometry = TRUE
           )) %>%
  bind_rows() %>% 
  write_sf('/tmp/median-hh-income-tract-usa.gpkg')

Leave a Reply

Your email address will not be published. Required fields are marked *