acs package in R: Cannot download dataset, error message is inscrutable - r

I am trying to use the acs package in R to download Census data for a basic map, but I am unable to download the data and I'm receiving a confusing error message.
My code is as follows:
#Including all packages here in case this is somehow the issue
install.packages(c("choroplethr", "choroplethrMaps", "tidycensus", "tigris", "leaflet", "acs", "sf"))
library(choroplethr)
library(choroplethrMaps)
library(tidycensus)
library(tigris)
library(leaflet)
library(acs)
library(sf)
library(tidyverse)
api.key.install("my_api_key")
SD_geo <- geo.make(state="CA", county = 73, tract = "*", block.group = "*")
median_income <- acs.fetch(endyear = 2015, span = 5, geography = SD_geo, table.number = "B19013", col.names="pretty")
Everything appears to work until the final command, when I receive the following error message:
trying URL 'http://web.mit.edu/eglenn/www/acs/acs-variables/acs_5yr_2015_var.xml.gz'
Content type 'application/xml' length 735879 bytes (718 KB)
downloaded 718 KB
Error in if (url.test["statusMessage"] != "OK") { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In (function (endyear, span = 5, dataset = "acs", keyword, table.name, :
XML variable lookup tables for this request
seem to be missing from ' https://api.census.gov/data/2015/acs5/variables.xml ';
temporarily downloading and using archived copies instead;
since this is *much* slower, recommend running
acs.tables.install()
This is puzzling to me because 1) it appears like something is in fact being downloaded at first? and 2) 'Error in if (url.test["statusMessage"] != "OK") { :
missing value where TRUE/FALSE needed' makes no sense to me. It doesn't align with any of the arguments in the function.
I have tried:
Downloading the tables using acs.tables.install() as recommended in the second half of the error message. Doesn't help.
Changing the endyear and span to be sure that I'm falling within the years of data supported by the API. I seem to be, according to the API documentation. Have also used the package default arguments with no luck.
Using 'variable =' and the code for the variable as found in the official API documentation. This returns only the two lines with the mysterious "Error in if..." message.
Removing colnames = "pretty"
I'm going to just download the datafile as a CSV and read it into R for now, but I'd like to be able to perform this function from the script for future maps. Any information on what's going on here would be appreciated. I am running R version 3.3.2. Also, I'm new to using this package and the API. But I'm following the documentation and can't find evidence that I'm doing anything wrong.
Tutorial I am working off of:
http://zevross.com/blog/2015/10/14/manipulating-and-mapping-us-census-data-in-r-using-the-acs-tigris-and-leaflet-packages-3/#get-the-tabular-data-acs
And documentation of the acs package: http://eglenn.scripts.mit.edu/citystate/wp-content/uploads/2013/02/wpid-working_with_acs_R2.pdf

To follow up on Brandon's comment, version 2.1.1 of the package is now on CRAN, which should resolve this issue.

Your code runs for me. My guess would be that the Census API was temporarily down.
As you loaded tidycensus and you'd like to do some mapping, you might also consider the following code:
library(tidycensus)
census_api_key("your key here") # use `install = TRUE` to install the key
options(tigris_use_cache = TRUE) # optional - to cache the Census shapefile
median_income <- get_acs(geography = "block group",
variables = "B19013_001",
state = "CA", county = "San Diego",
geometry = TRUE)
This will get you the data you need, along with feature geometry for mapping, as a tidy data frame.

I emailed Ezra Haber Glenn, the author of the package, about this as I was having the same issue. I received a response within 30 minutes and it was after midnight, which I thought was amazing. Long story short, the acs package version 2.1.0 is configured to work with the changes the Census Bureau is making to their API later this summer, and it is currently presenting some problems windows users in the mean time. Ezra is going to be releasing an update with a fix, but in the mean time I reverted back to version 2.0 and it works fine. I'm sure there are a few ways to do this, but I installed the devtools package and ran:
require(devtools)
install_version("acs", version = "2.0", repos = "http://cran.us.r-project.org")
Hope this helps anyone else having a similar issue.

Related

Error in CRS(slot(obj, "projargs")) : NA using adehabitatHR

I am trying to get home range data from a set of points using the package adehabitathr. Previously I was able to get all of the information in the form of SpatialPolygonsDataFrames. I wanted to export them as shapefiles, and attempted to use code I found on here
dir.create("esrishapes") #create new directory
writeOGR(obj = homerangeAll, dsn = "esrishapes", layer = "homerangeAll", driver = "ESRI
Shapefile")
But I was getting a projection error about not using PROJ6, adehabitathr uses PROJ4 so I attempted to download previous versions of sp and rgdal instead
require(devtools)
install_version("sp",version = "1.4-2")
install_version("rgdal",version = "1.5-8")
This didn't work even though the packages did install. Now I can't use any aspect of adehabitathr as it returns the error
Error in CRS(slot(obj, "projargs")) : NA
I don't have a strong understanding of coordinate systems so I have no idea where to begin fixing this.

R marmap getNOAA.bathy Error in if (ncol(x) == 3 & !exists("bathy", inherits = FALSE)) { : argument is of length zero

I've been trying to get bathymetry lines from marmap and recently got the following error message when getNOAA.bathy function is called
Querying NOAA database ...This may take seconds to minutes, depending on grid size
Error in if (ncol(x) == 3 & !exists("bathy", inherits = FALSE)) { : argument is of length zero
This happens even with something as simple as
map <- getNOAA.bathy(lon1=10,lon2=19,lat1=67,lat2=71,resolution=10, keep=TRUE)
I updated the package to the latest version (1.0.4) as I read that there could be issues related to server access. I've also tried running the above script in R rather than RStudio, but the error persists...
The function works fine if I use previously downloaded data, but now I needed use use another set of coordinates for a new map.
Any help is much appreciated!
You need to (re)install both rgdal and raster packages. This is already documented here on the GitHub Issue pages of the marmap package.
For anyone else looking, the reported error can also result from the server being down - R doesn't give any indication of this. You can check https://www.ncei.noaa.gov/alerts for scheduled outages.
For several months now, the same error message seem to have plagued Windows users, event with all packages up to date.
marmap v1.0.9 is now available on GitHub:
remotes::install_github("ericpante/marmap")
This version should solve this infamous Error message:
Error in if (ncol(x) == 3 & !exists("bathy", inherits = FALSE)) { :
argument is of length zero
I have been able to confirm that the error was due to a limitation in the length of urls that the raster package can handle on Windows. The geotif files from NOAA's servers are now downloaded with utils::download.file() in a temporary file on the user's disk and then imported in R using raster::raster().
marmap v1.0.9 will be available on CRAN servers in the next few days.

Error when using *whatNWISdata* function within *dataRetrieval* package from USGS: All components of query must be named

I am trying to use the whatNWISdata function to retrieve all available data for specific USGS sites.
I get the following error after trying to execute the function:
siteNo <- "09508300"
dailyDataAvailable <- whatNWISdata(siteNo, service = "dv", parameterCd = "00060",
statCd = "00003")
Yields:
Error: All components of query must be named
Although I am using the function as recommended in rdocumentation.org and cran, I get the same error. My rstudio is updated to the latest version and so is my dataRetrieval package that this function is part of.
This error was a question that was brought up in 2016 on github, and they recommended downgrading the httr package, but the httr package has been updated since this question was asked and it seems this issue was resolved in the update.
Thanks!
Thank you so much for your input #Dragonthoughts!
I emailed the author and apparently they had made some slight edits in the format of the input values, and yes you are correct in that the first argument needed a value as in the other inputs for the function.
For anyone else having this problem, it works when you change the input to be like this: dailyDataAvailable <- whatNWISdata(siteNumber = "09508300", service = "dv", parameterCd = "00060", statCd = "00003")

Fail to extract Zip Demographics Data using get_zip_demographics() function in ChoroplethrZip package

I am trying to get the zip demographics data of the year 2015. When I tried the codes below, an error message returns.
library(devtools)
install_github('arilamstein/choroplethrZip#v1.4.0')
library(choroplethrZip)
df_zip_2015 = get_zip_demographics(2015,5)
When the data tries to read in, I first got a few NAs introduced by coercion warnings, but then an error message returns
Error in choroplethr:::convert_acs_obj_to_df("zip", age, 1) :
argument "include_moe" is missing, with no default
Is there a way I can fix this?
It looks like you are installing choroplethrZip version 1.4.0. However, if you go to the choroplethrZip github page you will see that the latest version is actually 1.5.0.
When I run this code:
library(devtools)
install_github('arilamstein/choroplethrZip#v1.5.0')
library(choroplethrZip)
df_zip_2015 = get_zip_demographics(2015,5)
I do not get the error you describe.

tract_choropleth in Choroplethr v3.6.0 for R

I'm trying to replicate the maps shown in this short example (Choroplethr v3.6.0 is now on CRAN) for California, but I'm having trouble with the tract_choropleth function for areas that are not in NY. I get the error message below. I'm able to import the underlying tract data for all areas, but I'm having trouble mapping the non-NY ones. Thanks in advance.
# Load all packages (DONE)
# Install api key (DONE)
# Get Tract-Level Demographics
ca_stats = get_tract_demographics("california")
# Create choropleth map with the function tract_choropleth
ca_stats$value = ca_stats$per_capita_income
tract_choropleth(ca_stats, "california", title = "2013 Per Capita Income\nCensus Tracts", legend="Dollars")
Error message returned:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding,
use_iconv = use_iconv, : Cannot open layer In addition: Warning
message: In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
error 1 in extracting from zip file
I'm the author of the tigris package that choroplethr uses to get Census tract boundaries. That error shows up in the event of a corrupted download from the Census Bureau website. Ari's solution will work; you can also try:
library(tigris)
options(tigris_refresh = TRUE)
This will direct tigris to re-download the shapefile from the Census Bureau website, and allow you to still use caching. Once it is fixed, set options(tigris_refresh = FALSE) to control future re-downloads.
Thank you for using choroplethr.
I have never gotten this error myself, but it's possible that it relates to a caching issue with the tigris package. I say that because it looks very similar to this issue.
Here is the code which solved that user's problem:
library(tigris)
options(tigris_use_cache = FALSE)
That code causes tigris to not use its cache.
Please type that code into your R session and try the choroplethr code again.

Resources