R错误-“未提供编码:默认为UTF-8。错误:词法错误:json文本中的字符无效。”

时间:2018-09-12 20:48:39

标签: r api

我编写了下面的代码,以访问“ tidycensus”包中的普查数据,并且在大约7天内一切正常。代码没有更改,由于某种原因,有一天代码刚刚停止工作。我得到的错误是:

  

未提供编码:默认为UTF-8。
  错误:词法错误:json文本中的char无效。
     <html> <head> <titl
                      (就在这里)------ ^

我正在Mac Book Air-Mac OS Sierra 10.12.6上运行R和Rstudio的最新版本(截至2018年9月12日,它们是R 3.5.1和R studio 1.1.456)。请记住,我删除了API密钥,因此要测试代码,您需要在这里获得一个:https://api.census.gov/data/key_signup.html

# load the required packages
library(tidycensus)
library(tidyverse)
library(dplyr)
library(sp)
library(spatialEco)
library(rgdal)
library(rgeos)
library(tigris)
library(readr)
library(reshape)

### PART 1: retrieve the Census API data ###
# load the packman package (package management)
if(!require(pacman)){install.packages("pacman");
  library(pacman)}

# load the required pacakges
p_load(ggplot2, tidycensus, dplyr)

#input the API Key
#acs_key <- census_api_key("INSERT CENSUS KEY HERE", install = TRUE)
acs_key <- Sys.getenv("INSERT CENSUS KEY HERE")

# search for variables
# The function takes two required arguments: 
# the year of the Census or endyear of the ACS sample, and the dataset - one of "sf1", "sf3", or "acs5"
v15 <- load_variables(2016, "acs5", cache = TRUE)

# store the variable names to a vector 
# vul_vars <- c("B01003_001E","B02001_002E","B02001_003E","B02001_004E","B02001_005E","B02001_006E","B02001_007E","B02001_008E","B03002_012E","B01001_002E",
#              "B01001_026E","B01002_001E","B01002_002E","B01002_003E","B20002_001E","B20002_002E","B20002_003E","B08135_001E","B08006_001E","B08006_002E",
#              "B08006_003E","B08006_004E","B08006_008E","B08006_014E","B08006_015E","B08006_016E","B08006_018E","B08006_019E","B08006_020E","B08006_021E",
#              "B08006_025E","B08006_031E","B08006_032E","B08006_033E","B08006_035E","B08006_036E","B08006_037E","B08006_038E","B08006_042E","B08006_048E",
#              "B08006_049E","B08006_050E","B08121_001E","B08121_002E","B08121_003E","B08121_004E","B08121_005E","B08121_006E","B09008_002E","B09008_004E",
#              "B09008_005E","B09010_002E","B09010_003E","B09010_004E","B09010_005E","B09010_006E","B09010_007E","B09019_002E","B09019_003E","B09019_005E",
#              "B09019_006E","B09019_007E","B09019_008E","B09019_024E","B09019_038E","B15003_002E","B15003_016E","B15003_017E","B15003_018E","B15003_020E",
#              "B15003_022E","B15003_023E","B15003_025E","B16001_002E","B16001_002E","B16001_002E","B23025_004E","B23025_005E","B17020_002E","B08201_002E",
#              "B08201_002E","B19013_001E","B19013A_001E","B19013B_001E")

vul_vars <- c("B01003_001","B02001_002","B02001_003","B02001_004","B02001_005","B02001_006","B02001_007","B02001_008","B03002_012","B01001_002",
             "B01001_026","B01002_001","B01002_002","B01002_003","B20002_001","B20002_002","B20002_003","B08135_001","B08006_001","B08006_002",
             "B08006_003","B08006_004","B08006_008","B08006_014","B08006_015","B08006_016","B08006_018","B08006_019","B08006_020","B08006_021",
             "B08006_025","B08006_031","B08006_032","B08006_033","B08006_035","B08006_036","B08006_037","B08006_038","B08006_042","B08006_048",
             "B08006_049","B08006_050","B08121_001","B08121_002","B08121_003","B08121_004","B08121_005","B08121_006","B09008_002","B09008_004",
             "B09008_005","B09010_002","B09010_003","B09010_004","B09010_005","B09010_006","B09010_007","B09019_002","B09019_003","B09019_005",
             "B09019_006","B09019_007","B09019_008","B09019_024","B09019_038","B15003_002","B15003_016","B15003_017","B15003_018","B15003_020",
             "B15003_022","B15003_023","B15003_025","B16001_002","B16001_002","B16001_002","B23025_004","B23025_005","B17020_002","B08201_002",
             "B08201_002","B19013_001","B19013A_001","B19013B_001")

vul_vars <- unique(vul_vars)
# grab the data for all of the states
vul_acs <-
  get_acs(geography = "tract", variables = vul_vars,
          state = c("NY","IL","DC"), output = "wide")

1 个答案:

答案 0 :(得分:0)

所以我只是遇到了一个与您类似的问题,发现我已经复制了一段并将其粘贴到我的api密钥的末尾,这导致它生成相同的错误。尽管基于我最近遇到的相同错误的猜测,但请尝试检查api密钥末尾是否存在意外期限并将其删除。如果是导致错误的原因,那么其余的都应该可以正常工作,因为我也刚刚测试了您的代码,对我来说也很好