我正在尝试对data.frame的每一行进行API调用,同时将行的值作为参数传递。应该将返回值添加到我的框架
但是,我无法使我的代码工作。任何帮助表示赞赏。
APPLYapiCallTest.csv
的内容:
id title returnValue
01 "wine" ""
02 "beer" ""
03 "coffee" ""
基本上使用call("wine)
调用API,并将空字段替换为结果,例如饮料
这是我到目前为止所得到的。
#call api per row using apply
library(jsonlite)
library(httr)
callAPI <- function(x) {
findWhat <- as.character(x)
#create ULR
url1 <- "http://api.nal.usda.gov/ndb/search/?format=json&q="
url2 <- "&sort=n&max=1&offset=0&api_key=KYG9lsu0nz31SG5yHGdAsM28IuCEGxWWlvdYqelI&location=Chicago%2BIL"
fURL <- paste(url1, findWhat, url2, sep="")
apiRet <- data.frame(fromJSON(txt=fURL, flatten = TRUE))
result <- apiRet[1,c(9)]
return(result)
}
tData <- data.frame(read.delim("~/Documents/R-SCRIPTS/DATA/APPLYapiCallTest"))
apply(tData[,c('title')], 1, function(x) callAPI(x) )
答案 0 :(得分:2)
我认为你最好做这样的事情:
library(jsonlite)
library(httr)
library(pbapply)
callAPI <- function(x) {
res <- GET("http://api.nal.usda.gov/ndb/search/",
query=list(format="json",
q=x,
sort="n",
offset=0,
max=16,
api_key=Sys.getenv("NAL_API_KEY"),
location="Berwick+ME"))
stop_for_status(res)
return(data.frame(fromJSON(content(res, as="text"), flatten=TRUE), stringsAsFactors=FALSE))
}
tData <- data.frame(id=c("01", "02", "03"),
title=c("wine", "beer", "coffee"),
returnValue=c("", "", ""),
stringsAsFactors=FALSE)
dat <- merge(do.call(rbind.data.frame, pblapply(tData$title, callAPI)),
tData[, c("title", "id")], by.x="list.q", by.y="title", all.x=TRUE)
str(dat)
## 'data.frame': 45 obs. of 12 variables:
## $ list.q : chr "beer" "beer" "beer" "beer" ...
## $ list.sr : chr "28" "28" "28" "28" ...
## $ list.start : int 0 0 0 0 0 0 0 0 0 0 ...
## $ list.end : int 13 13 13 13 13 13 13 13 13 13 ...
## $ list.total : int 13 13 13 13 13 13 13 13 13 13 ...
## $ list.group : chr "" "" "" "" ...
## $ list.sort : chr "n" "n" "n" "n" ...
## $ list.item.offset: int 0 1 2 3 4 5 6 7 8 9 ...
## $ list.item.group : chr "Beverages" "Beverages" "Beverages" "Beverages" ...
## $ list.item.name : chr "Alcoholic beverage, beer, light" "Alcoholic beverage, beer, light, BUD LIGHT" "Alcoholic beverage, beer, light, BUDWEISER SELECT" "Alcoholic beverage, beer, light, higher alcohol" ...
## $ list.item.ndbno : chr "14006" "14007" "14005" "14248" ...
## $ id : chr "02" "02" "02" "02" ...
这样,您就可以通过httr::GET
为title
中的每个tData
进行结构化调用,然后将所有结果绑定到一个数据框中,最后添加回ID。
这也允许您将NAL_API_KEY
放入.Renviron
并避免将其暴露在您的工作流程中(以及SO: - )
您可以删除函数内部或外部不需要的API结果响应。