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

Errors with ACS #146

Closed mcgaugj closed 5 years ago

mcgaugj commented 5 years ago

Hi There. I'm a bit of a novice so please forgive me if this is super basic. I have tried troubleshooting on my own and this is my last resort. I'm simply trying to get some data out of the API and I don't even get that far. Here is what I am running:

library(tidyvers) library(tidycensus) library(sf)

census_api_key("XXXXXXXXXXXXXXXXXXXXXXXXXXXX", install = TRUE)

readRenviron("~/.Renviron")

Sys.getenv("CENSUS_API_KEY")

ACS1 <- get_acs(geography = "tract", survey = "acs1", table = "B19001", state = "CA", county = "Alameda", geometry = TRUE)

ACS2 <- load_variables(2017, "acs5", cache = TRUE)

And here is the error messages:

ACS1 <- get_acs(geography = "tract", survey = "acs1", table = "B19001", state = "CA", county = "Alameda", geometry = TRUE) The one-year ACS provides data for geographies with populations of 65,000 and greater. Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set options(tigris_use_cache = TRUE). Loading ACS1 variables for 2015 from table B19001. To cache this dataset for faster access to ACS tables in the future, run this function with cache_table = TRUE. You only need to do this once per ACS dataset. Using FIPS code '06' for state 'CA' Using FIPS code '001' for 'Alameda 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' ACS2 <- load_variables(2017, "acs5", cache = TRUE) Error: lexical error: invalid char in json text.

Error report (right here) ------^ </blockquote> <p>Do you have any idea what I'm doing wrong?</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mfherman"><img src="https://avatars.githubusercontent.com/u/24945757?v=4" />mfherman</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>Looks like the issue is that you're trying to get tract-level data from the 1-year ACS estimates, but this data is only available as a 5-year estimate. You can look <a href="https://www.census.gov/programs-surveys/acs/geography-acs/areas-published.html">at this table from the Census Bureau</a> (admittedly this is very poorly formatted) to see what geographies are available as 1- or 5-year estimates.</p> <p>Generally, the ACS provides 1-year estimates for areas with more than 65,000 people -- so this could be counties, congressional districts, PUMAs, etc. But for tracts, block groups, and other small area geographies, you have to use the 5-year estimates.</p> <p>See below for an example of pulling tract-level estimates from the 5-year ACS:</p> <pre><code class="language-r">library(tidycensus) ACS1 <- get_acs( geography = "tract", survey = "acs5", table = "B19001", state = "CA", county = "Alameda", geometry = TRUE ) #> Getting data from the 2013-2017 5-year ACS head(ACS1) #> Simple feature collection with 6 features and 5 fields #> geometry type: MULTIPOLYGON #> dimension: XY #> bbox: xmin: -122.2469 ymin: 37.84996 xmax: -122.2124 ymax: 37.88544 #> epsg (SRID): 4269 #> proj4string: +proj=longlat +datum=NAD83 +no_defs #> GEOID NAME variable #> 1 06001400100 Census Tract 4001, Alameda County, California B19001_001 #> 2 06001400100 Census Tract 4001, Alameda County, California B19001_002 #> 3 06001400100 Census Tract 4001, Alameda County, California B19001_003 #> 4 06001400100 Census Tract 4001, Alameda County, California B19001_004 #> 5 06001400100 Census Tract 4001, Alameda County, California B19001_005 #> 6 06001400100 Census Tract 4001, Alameda County, California B19001_006 #> estimate moe geometry #> 1 1254 65 MULTIPOLYGON (((-122.2469 3... #> 2 24 24 MULTIPOLYGON (((-122.2469 3... #> 3 24 22 MULTIPOLYGON (((-122.2469 3... #> 4 8 13 MULTIPOLYGON (((-122.2469 3... #> 5 14 16 MULTIPOLYGON (((-122.2469 3... #> 6 52 45 MULTIPOLYGON (((-122.2469 3...</code></pre> <p><sup>Created on 2019-02-01 by the <a href="https://reprex.tidyverse.org">reprex package</a> (v0.2.1)</sup></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/abuabara"><img src="https://avatars.githubusercontent.com/u/12164393?v=4" />abuabara</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>1) first of all, it is tidyverse .. you missed the "e" library(tidyverse)</p> <p>2) this variable does not exist in the ACS1 survey. But ACS5 it works</p> <p>3) what you are getting ACS2 is just the library list ... what is fine, it supposedly works</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mcgaugj"><img src="https://avatars.githubusercontent.com/u/7540163?v=4" />mcgaugj</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>Thanks both. I fixed the typo in the package and then switched to the acs5, but it's throwing errors in both (I know the second one only pulls the variables). Both say there are invalid characters in the JSON. Here's the errors:</p> <blockquote> <p>ACS1 <- get_acs(geography = "tract", survey = "acs5", table = "B19001", state = "CA", county = "Alameda", geometry = TRUE) Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set <code>options(tigris_use_cache = TRUE)</code>. Loading ACS5 variables for 2015 from table B19001. To cache this dataset for faster access to ACS tables in the future, run this function with <code>cache_table = TRUE</code>. You only need to do this once per ACS dataset. Error: lexical error: invalid char in json text.</p> <html><head><title>Error report (right here) ------^ ACS2 <- load_variables(2017, "acs5", cache = TRUE) Error: lexical error: invalid char in json text. <html><head><title>Error report (right here) ------^ </blockquote> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mfherman"><img src="https://avatars.githubusercontent.com/u/24945757?v=4" />mfherman</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>Hmmm. It seems like you're having trouble parsing the JSON that is returned from the Census API. You could try updating your version of <code>jsonlite</code> -- I have the most recent CRAN version (1.6) and everything appears to be working fine on my end. Also, can you paste the results of <code>devtools::session_info()</code> or <code>sessionInfo()</code></p> <pre><code>install.packages(jsonlite)</code></pre> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/abuabara"><img src="https://avatars.githubusercontent.com/u/12164393?v=4" />abuabara</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>yep, would say the same</p> <p>considering this: <a href="https://github.com/walkerke/tidycensus/issues/117">https://github.com/walkerke/tidycensus/issues/117</a></p> <p>I think you could be using some old package stuff</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/walkerke"><img src="https://avatars.githubusercontent.com/u/4872833?v=4" />walkerke</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>@mcgaugj did you activate your API key? That can sometimes trigger an error like this. If not, go back to the email from Census where you received your key and click the link in the email to activate it. </p> <p>Otherwise, make sure that you are running the latest version of tidycensus from CRAN - this will be version 0.9. </p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mcgaugj"><img src="https://avatars.githubusercontent.com/u/7540163?v=4" />mcgaugj</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>It was the jsonlite package. I was on an old version. I'm in business gentleman! Thank you very much for your help. It is much appreciated.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mfherman"><img src="https://avatars.githubusercontent.com/u/24945757?v=4" />mfherman</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>Glad you got it working, @mcgaugj! Do you happen to know which version of jsonlite you had installed before you updated? </p> <p>@walkerke, perhaps we should specify a minimum required version of jsonline in imports?</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mcgaugj"><img src="https://avatars.githubusercontent.com/u/7540163?v=4" />mcgaugj</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>It was 1.5 (one version made a difference).</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/mfherman"><img src="https://avatars.githubusercontent.com/u/24945757?v=4" />mfherman</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <p>Hmmm...I just installed jsonlite 1.5 on my machine and I'm not getting the parsing error you got, @mcgaugj . Anyhow, glad it's working for you now!</p> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>