xml - R : "subscript out of bounds" 中的数据混搭

标签 xml r http yahoo

我正在通过“R 中的数据混搭”学习 R,但无法获得第 1 页上的示例。 5 上类。我使用的代码是这样的:

# Install and load RCurl
install.packages("RCurl")
library("RCurl")

# Install and load the XML package
install.packages("XML")
library("XML")

# Download file and parse it
appid<-    'ucVVQzLV34GQR4ppLwbdW6G8cCSZDoCBqAc53NXsWB3gXkmP1I4epLwMxboV.PfADi_2ubr2A7Cg8FO4Z3xVxxujza2FJ    8M-' 
street<-"11408 Bellflower Road" 
RCurl<-paste( 
"http://local.yahooapis.com/MapsService/V1/geocode?appid=", 
appid, 
"&street=",
URLencode(street), 
"&city=Cleveland&state=OH" 
,sep="") 
#xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE) 
xmlResult<-xmlTreeParse(getURL(RCurl))

#Print the output
str(xmlResult)

但是当我这样做时,我没有得到以下结果:

List of 2
 $ doc:List of 3
  ..$ file    :List of 2
  .. ..$ text   : Named list()
  .. .. ..- attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode" "RXMLAbstractNode"     "XMLAbstractNode" ...
  .. ..$ Message:List of 1
  .. .. ..$ text: Named list()
  .. .. .. ..- attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode" "RXMLAbstractNode"    "XMLAbstractNode" ...
  .. .. ..- attr(*, "class")= chr [1:4] "XMLNode" "RXMLAbstractNode" "XMLAbstractNode"   "oldClass"
  .. ..- attr(*, "class")= chr [1:4] "XMLNode" "RXMLAbstractNode" "XMLAbstractNode"   "oldClass"
  ..$ version : Named list()
  .. ..- attr(*, "class")= chr [1:5] "XMLCommentNode" "XMLNode" "RXMLAbstractNode"   "XMLAbstractNode" ...
  ..$ children:
Error in x$children[[...]] : subscript out of bounds

我做错了什么?

最佳答案

该 API 已被弃用。 Here is the current one .试试这个:

library("RCurl")
library("XML")
appid      <- 'ucVVQzLV34GQR4ppLwbdW6G8cCSZDoCBqAc53NXsWB3gXkmP1I4epLwMxboV.PfADi_2ubr2A7Cg8FO4Z3xVxxujza2FJ    8M-' 
address    <- paste("11408+Bellflower+Road", "Cleveland", "OH", sep=",+")
urlRequest <- paste("http://where.yahooapis.com/geocode?q=", 
                    address, appid=appid, sep = "")
doc <- xmlToList(xmlTreeParse(urlRequest)) # Convert to List
str(doc)

它对我有用。不过,您可能想检查它是否有正确的地址。没有邮政编码吗?您可能还想查看 Google's Geocoding API .它不再需要烦人的 key 。

List of 7
 $ Error       : chr "0"
 $ ErrorMessage: chr "No error"
 $ Locale      : chr "us_US"
 $ Quality     : chr "87"
 $ Found       : chr "1"
 $ Result      :List of 29
  ..$ quality     : chr "87"
  ..$ latitude    : chr "41.511326"
  ..$ longitude   : chr "-81.605583"
  ..$ offsetlat   : chr "41.511230"
  ..$ offsetlon   : chr "-81.605453"
  ..$ radius      : chr "2900"
  ..$ name        : NULL
  ..$ line1       : chr "11408 Bellflower Rd"
  ..$ line2       : chr "Cleveland, OH  44106"
  ..$ line3       : NULL
  ..$ line4       : chr "United States"
  ..$ house       : chr "11408"
  ..$ street      : chr "Bellflower Rd"
  ..$ xstreet     : NULL
  ..$ unittype    : NULL
  ..$ unit        : NULL
  ..$ postal      : chr "44106"
  ..$ neighborhood: NULL
  ..$ city        : chr "Cleveland"
  ..$ county      : chr "Cuyahoga County"
  ..$ state       : chr "Ohio"
  ..$ country     : chr "United States"
  ..$ countrycode : chr "US"
  ..$ statecode   : chr "OH"
  ..$ countycode  : NULL
  ..$ uzip        : chr "44106"
  ..$ hash        : chr "BFBDCAB96C2CB175"
  ..$ woeid       : chr "12776632"
  ..$ woetype     : chr "11"
 $ .attrs      : Named chr "1.0"
  ..- attr(*, "names")= chr "version"

关于xml - R : "subscript out of bounds" 中的数据混搭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059730/

相关文章:

php - 为什么要使用 XML 解析器?

javascript - html5 + XML + JS 的动态页面?

javascript - innerHTML 用于查看 div 中的 XML 内容

arrays - 在 R 中合并两个数组

Angular 2 Http 请求不发送凭据

wpf - 为什么 XML 命名空间通常以 http ://. .. 开头?

r - 评估 R 中函数调用内的代码(在循环内使用 ICC::ICCbare)

r - 将 Rscript 中的 bool 值输出到 Bash 变量中

api - 何时选择关联而非查询

java - Retrofit:如何发送带有常量字段的 POST 请求?