R中的acs包:无法下载数据集,错误消息难以理解



我正在尝试使用 R 中的 acs 包下载基本地图的人口普查数据,但我无法下载数据,并且收到一条令人困惑的错误消息。

我的代码如下:

#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")

一切似乎都正常工作,直到最后一个命令,当我收到以下错误消息时:

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()

这让我感到困惑,因为 1( 一开始看起来好像正在下载某些东西?和 2( 'if 中的错误 (url.test["statusMessage"] != "OK"( { : 需要真/假的地方缺少值对我来说毫无意义。它与函数中的任何参数都不一致。

我试过:

  • 按照错误消息后半部分的建议使用 acs.tables.install(( 下载表。无济于事。

  • 更改结束年份和跨度以确保我属于 API 支持的数据年份。根据 API 文档,我似乎是。也使用了包默认参数,但没有运气。

  • 使用"变量 ="和官方 API 文档中的变量代码。这只返回带有神秘"如果错误..."的两行消息。

  • 删除串名 ="漂亮">

我现在只将数据文件下载为 CSV 并将其读取到 R 中,但我希望能够从脚本中执行此功能以用于将来的地图。有关这里发生的事情的任何信息将不胜感激。我正在运行 R 版本 3.3.2。另外,我是使用此包和API的新手。但我正在关注文档,找不到我做错任何事的证据。

我正在处理的教程: 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

和 acs 包的文档:http://eglenn.scripts.mit.edu/citystate/wp-content/uploads/2013/02/wpid-working_with_acs_R2.pdf

为了跟进 Brandon 的评论,该软件包的 2.1.1 版本现在在 CRAN 上,应该可以解决此问题。

你的代码为我运行。 我的猜测是人口普查API暂时关闭了。

当您加载tidycensus并希望执行一些映射时,您可能还考虑以下代码:

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)

这将为您提供所需的数据以及用于映射的要素几何,作为整洁的数据框。

我通过电子邮件向软件包的作者Ezra Haber Glenn发送了有关此内容的信息,因为我遇到了同样的问题。 我在30分钟内收到了回复,那是午夜之后,我认为这很神奇。 长话短说,acs 包版本 2.1.0 配置为与人口普查局在今年夏天晚些时候对其 API 所做的更改一起使用,并且它目前同时存在一些问题 Windows 用户。 Ezra 将发布带有修复程序的更新,但与此同时,我恢复到 2.0 版并且它工作正常。 我确定有几种方法可以做到这一点,但我安装了 devtools 包并运行了:

需求(开发工具(

install_version("acs", version = "2.0", repos = "http://cran.us.r-project.org"(

希望这有助于其他有类似问题的人。

最新更新