简体   繁体   中英

Data mashups in R: "subscript out of bounds"

I'm learning R with "Data mashups in R" and I can't get the example on p. 5 to work. The code I use is this:

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

But when I do this I don't get the following result:

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

What am I doing wrong?

That API has been deprecated. Here is the current one . Try this instead:

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)

It worked for me. Though, you might want to check it got the right address. Is there no ZIP code? You might also want to look into Google's Geocoding API . It doesn't require an annoying key anymore.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM