I am attempting to utilize the Census Bureau's batch geocoder (http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf) with R. The input addresses are in a data frame, not a CSV. As the addresses are an intermediate step, I do not want to write them to a CSV.
I have read several postings on stackoverflow. Hadley's solution (Posting to and Receiving data from API using httr in R) illustrates how to upload an existing CSV file. MrFlick's solution (Uploading a csv to an api in R) seems close to what I want but uses a string, not a data frame.
Here's what I have in the way of code:
#generate data frame of test addresses for this example
a = c(1, 2, 3)
b = c("125 Worth Street", "258 Broadway", "8 Centre Street")
c = rep("New York", 3)
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)
#names specified by API documentation
colnames(addresses) <- c("Unique ID","Street address","City","State","ZIP")
apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
req <- POST(apiurl, body=list(
addressFile = RCurl::fileUpload(
filename = "test.csv",
contents = addresses
),
benchmark = "Public_AR_Census2010",
vintage = "Census2010_Census2010"
),
encode="multipart"
)
stop_for_status(req)
Thanks in advance.
if you're willing to write the data to a temp file...