Reputation: 6104
i am trying to automate the download of a bit of data from the API of this government website.
the website instructs:
All requests for this version should be made to the following URL:
http://api.finder.healthcare.gov/v2.0/
i can find plenty of information about how to send xml requests, but none of the examples are R
-specific.. and there's plenty of R code out there that shows how to use the XML
, httr
, and RCurl
packages, but i couldn't find any examples on SO or the r-help mailing list of how to actually send an xml request. ..there's more documentation for parsing the response.
on the government website, if you click the PlansForIndividualOrFamily Samples
example, it displays the xml request (code below) that needs to be sent.
url <- "http://api.finder.healthcare.gov/v2.0/"
xml.request <-
"<?xml version='1.0' encoding='UTF-8'?>
<PrivateOptionsAPIRequest>
<PlansForIndividualOrFamilyRequest>
<Enrollees>
<Primary>
<DateOfBirth>1990-01-01</DateOfBirth>
<Gender>Male</Gender>
<TobaccoUser>Smoker</TobaccoUser>
</Primary>
</Enrollees>
<Location>
<ZipCode>69201</ZipCode>
<County>
<CountyName>CHERRY</CountyName>
<StateCode>NE</StateCode>
</County>
</Location>
<InsuranceEffectiveDate>2012-10-01</InsuranceEffectiveDate>
<IsFilterAnalysisRequiredIndicator>false</IsFilterAnalysisRequiredIndicator>
<PaginationInformation>
<PageNumber>1</PageNumber>
<PageSize>10</PageSize>
</PaginationInformation>
<SortOrder>
<SortField>OOP LIMIT - INDIVIDUAL - IN NETWORK</SortField>
<SortDirection>ASC</SortDirection>
</SortOrder>
<Filter/>
</PlansForIndividualOrFamilyRequest>
</PrivateOptionsAPIRequest>"
Upvotes: 8
Views: 6723
Reputation: 121568
Using RCurl
you do this ;
myheader=c(Connection="close",
'Content-Type' = "application/xml",
'Content-length' =nchar(xml.request))
data = getURL(url = url,
postfields=xml.request,
httpheader=myheader,
verbose=TRUE)
That's about it. Then You can using xpathApply
of XML package to retrieve data. For example to get families ID:
library(XML)
xmltext <- xmlTreeParse(data, asText = TRUE,useInternalNodes=T)
unlist(xpathApply(xmltext,'//Plan/PlanID',xmlValue)) ## change the right xpath here
"29678NE0780012" "29678NE0780011" "29678NE0140010"
"29678NE0780010" "29678NE0140019" "29678NE0140018" "29678NE0140017"
"29678NE0140016" "29678NE0780005" "29678NE0780004"
Upvotes: 12