首页 > 解决方案 > 将 df 帧值输入到 GET 函数 Web 查询中

问题描述

我正在尝试将数据框中的值列表输入到我的 get 函数中以进行 Web 查询,然后在每次迭代中循环。如果有人能够链接我一些进一步的资源来阅读和学习,将不胜感激。

以下是从 API 服务器中提取数据名称的代码。我计划使用purrr迭代函数来检查它。列表中的输入将插入到变量 nameRFG_SELECT中。

library(httr)
library(purrr)

## Call up Query Development Script

## Calls up every single rainfall data gauge across the entirety of QLD

wmip_callup <- GET('https://water-monitoring.information.qld.gov.au/cgi/webservice.pl?{"function":"get_site_list","version":"1","params":{"site_list":"MERGE(GROUP(MGR_OFFICE_ALL,AYR),GROUP(MGR_OFFICE_ALL,BRISBANE),GROUP(MGR_OFFICE_ALL,BUNDABERG),GROUP(MGR_OFFICE_ALL,MACKAY),GROUP(MGR_OFFICE_ALL,MAREEBA),GROUP(MGR_OFFICE_ALL,ROCKHAMPTON),GROUP(MGR_OFFICE_ALL,SOUTH_JOHNSTONE),GROUP(MGR_OFFICE_ALL,TOOWOOMBA))"}}')

# Turns API server data into JSON data.

wmip_dataf <- content(wmip_callup, type = 'application/json')

# Returns the values of the rainfall gauge site names and is the directory function. 

list_var <- wmip_dataf[["_return"]][["sites"]]

# Combines all of the rainfall gauge data together in a list (could be used for giving file names / looping the data).

rfg_bind <- do.call(rbind.data.frame, list_var)

# Sets the column name of the combination data frame. 

rfg_bind <- setNames(rfg_bind, "Rainfall Gauge Name")                   

rfg_select <- rfg_bind$`Rainfall Gauge Name`

# Attempts to filter list into query:

wmip_input <- GET('https://water-monitoring.information.qld.gov.au/cgi/webservice.pl?{"function":"get_ts_traces","version":"1","params":{"site_list":**rfg_select**,"datasource":"AT","varfrom":"10","varto":"10","start_time":"0","end_time":"0","data_type":"mean","interval":"day","multiplier":"1"}}') ``` 


标签: rpurrrhttr

解决方案


嘿,

经过一些工作,我找到了使用连接字符串的解决方案。

我设置了一个虚拟变量来帮助我选择数据值。

# Dummy Variable string:

wmip_url <- 'https://water-monitoring.information.qld.gov.au/cgi/webservice.pl?{"function":"get_ts_traces","version":"1","params":{"site_list":"varinput","datasource":"AT","varfrom":"10","varto":"10","start_time":"0","end_time":"0","data_type":"mean","interval":"day","multiplier":"1"}}'


# Dummy String, grabs ones value from the list.

rfg_individual <- rfg_select[2:2]

# Replaces the specified input 
rfg_replace <- gsub("varinput", rfg_individual, wmip_url)

# Result 

"https://water-monitoring.information.qld.gov.au/cgi/webservice.pl?{\"function\":\"get_ts_traces\",\"version\":\"1\",\"params\":{\"site_list\":\"001203A\",\"datasource\":\"AT\",\"varfrom\":\"10\",\"varto\":\"10\",\"start_time\":\"0\",\"end_time\":\"0\",\"data_type\":\"mean\",\"interval\":\"day\",\"multiplier\":\"1\"}}"


推荐阅读