我试图使用人口普查局的批处理地理编码器(http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf)与R。输入地址位于 Dataframe 中,而不是CSV中。由于地址是一个中间步骤,我不想将它们写入CSV。
我在stackoverflow上读过几篇文章。Hadley的解决方案(Posting to and Receiving data from API using httr in R)演示了如何上传现有的CSV文件。MrFlick的解决方案(Uploading a csv to an api in R)似乎与我想要的很接近,但它使用的是字符串,而不是 Dataframe 。
下面是我的代码:
#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)
先谢了。
2条答案
按热度按时间jdzmm42g1#
如果你愿意把数据写到临时文件里
qf9go6mv2#
如果有人在尝试这些代码时收到错误,那是因为普查API从http切换到https。将
apiurl
调整为基于https,它应该可以工作。