walkerke / tidycensus

Load US Census boundary and attribute data as 'tidyverse' and 'sf'-ready data frames in R
https://walker-data.com/tidycensus
Other
639 stars 100 forks source link

cannot coerce type 'closure' to vector of type 'character' ... Round 2 #40

Closed aeonstone closed 6 years ago

aeonstone commented 6 years ago

I saw a similar issue as a closed issue. I commented as a follow up to the closed issue but I thought it might be helpful to post this as a new issue as well. My apologies if that's too much.

I am experiencing an issue when submitting a census call. I'm getting the following error: Client error Bad Request Client error: (400) Bad Request. I've posted my code below.

Here is what I am running

library(tidycensus)
library(tidyverse)
census_api_key("xxxxx")
age <- get_acs(table = "B01001", state = "SD", county = "Custer", geography = "county") 

And here is what I get back

> age <- get_acs(table = "B01001", state = "SD", county = "Custer", geography = "county")
Using FIPS code '46' for state 'SD'
Using FIPS code '033' for 'Custer County'
[1] "Client error Bad Request Client error: (400) Bad Request"
Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'

I was able to get df2 <- get_acs(geography = "county", table = "B01001", survey = "acs5", year = 2015, state = "SD") to run without issue. But that call returns all counties in the state.

The following which calls for all tracks within a county also seems to work df2 <- get_acs(geography = "tract", table = "B01001", survey = "acs5", year = 2015, state = "SD") It seems like the error occurs when trying to pull in data for a single county within a state. Is this a feature or a bug?

walkerke commented 6 years ago

Right now, it's a feature. My design philosophy with tidycensus is not to expose every possible query available from the Census/ACS APIs, but rather "do enough" to get Census data ready for use in R with tidyverse functions. As such, the county parameter works for geographies that nest within counties - specifically tracts, block groups, and blocks - but not counties themselves, which are available by state.

If you need a single county, dplyr will get you there with one line of extra code:

age <- get_acs(geography = "county", table = "B01001", state = "SD") %>%
  filter(grepl("Custer", NAME))

If you require a package that fully exposes the Census API, I recommend the censusapi package: https://github.com/hrecht/censusapi.

I plan to document this more/add some more informative error messages in a future release.

aeonstone commented 6 years ago

Thank you for the reply. I can understand the design choice. And thank you for the tip on how to get at what I'm after. I just found your work and I'm really excited to dive in and learn more about how to use it. Thanks for the great work!

walkerke commented 6 years ago

Awesome - thanks for using the package!

walkerke commented 6 years ago

I've been giving this some thought and I've now built this functionality into the package as I could see it being a common request. So now, this works:

library(tidycensus)

age <- get_acs(table = "B01001", state = "SD", county = "Custer", geography = "county") 

age

# A tibble: 49 x 5
   GEOID                        NAME   variable estimate   moe
   <chr>                       <chr>      <chr>    <dbl> <dbl>
 1 46033 Custer County, South Dakota B01001_001     8447    NA
 2 46033 Custer County, South Dakota B01001_002     4241    87
 3 46033 Custer County, South Dakota B01001_003      171    15
 4 46033 Custer County, South Dakota B01001_004      233    48
 5 46033 Custer County, South Dakota B01001_005      185    52
 6 46033 Custer County, South Dakota B01001_006      205    39
 7 46033 Custer County, South Dakota B01001_007       42    25
 8 46033 Custer County, South Dakota B01001_008       11    11
 9 46033 Custer County, South Dakota B01001_009       25    33
10 46033 Custer County, South Dakota B01001_010      117    46
# ... with 39 more rows