Error in Connecting with US Census API from the wru R Package

107 views Asked by At

I'm trying to use the wru R package for Bayesian Improved Surname Geocoding (BISG). I have all of my data geocoded, but the problem is with obtaining the census data through the get_census_data function.

CensusObj <- get_census_data(key = "CENSUS_API_KEY", 
             state = "NM",
             census.geo = "tract",
             retry = 2,
             year = 2010
             )

Every time I run this code, I get the following error (key removed):

Error in file(con, "r") : 
  cannot open the connection to 'https://api.census.gov/data/2010/dec/sf1?key=...&get=P005003,P005004,P005005,P005006,P005007,P005008,P005009,P005010&for=county:*&in=state:35'
In addition: Warning message:
In file(con, "r") :
  URL 'https://api.census.gov/data/2010/dec/sf1?key=...&get=P005003,P005004,P005005,P005006,P005007,P005008,P005009,P005010&for=county:*&in=state:35': status was 'SSL connect error'
Try census server again: https://api.census.gov/data/2010/dec/sf1?

My key isn't the problem as it works with other Census functions.

I can calculate the rates of each race per census tract myself using the variables found in the error code, but I don't know how the wru package formats the data to be able to read it. The code below gives the probability of each race in a given census tract.

library(tidycensus)
libryar(dplyr)

desired_vars = c(
"prop.white" = "P005003",
"prop.black" = "P005004",
"Native American" = "P005005", 
"prop.asian" = "P005006",
"prop.Native_Hawaiian" = "P005007",
"prop.other" = "P005008",
"prop_two_races" = "P005009",
"prop_hispanic" = "P005010"
)

race= get_decennial(
  geography = "tract",
  state = "NM",
  variables = desired_vars,
  summary_var = "P005001", 
  year = 2010
)

race_rate <- race %>%  
  rename(population = summary_value) %>%
  group_by(GEOID) %>%
  mutate(rate = (value/population)) %>%
  ungroup(GEOID)

rate_wide <- race_rate %>%
  group_by(GEOID) %>%
  select(-NAME, -value, -population) %>%
  tidyr::pivot_wider(names_from = variable, values_from = rate) %>%
  mutate(state = "NM",
         county = substr(GEOID, 3, 5),
         tract = substr(GEOID, 6, 11)
        ) %>%
  ungroup(GEOID)%>%
  select(-GEOID)

Any ideas on resolving this error or how to manually pull the census data and format it myself to be able to run it in the wru package are appreciated.

0

There are 0 answers