我正在尝试编写一个构建API请求的函数。困难:我想在函数内部使用最佳参数,并根据这些可选参数的存在来构建API请求。示例:
space <- "earth"
start_date <- "2018-10"
end_date <- "2018-11"
optionalArgument1 <- "America"
optionalArgument2 <- "people"
optionalArgument3 <- "size"
optionalArgument4 <- "ocean"
myfunction <- function(space, start_date, end_date, optionalArgument1, optionalArgument2, optionalArgument3, optionalArgument4){
api_request <- paste0("https://myapi.com/getData?hello",
#NEEDED
"&space={s:'", space,"'}",
"&period={M:{start:'",start_date,"',end:'",end_date,"'}}",
#OPTIONAL
"&filter={",
"SMALL_filter1:{$sf1:'",optionalArgument1,"'},",
"SMALL_filter2:{$sf2:'",optionalArgument2,"'},",
"SMALL_filter3:{$sf3:'",optionalArgument3,"'}",
"}",
"&segment=",optionalArgument4
)
return(api_request)
}
我的目标是允许函数传递api_request,无论用户提供1、2、3还是4个可选参数。
例如:
myfunction(space, start_date, end_date, optionalArgument1, optionalArgument4)
应返回如下内容:
[1] https://myapi.com/getData?hello&space={s:'earth'}&period={M:{start:'2018-10'end:'2018-11'}}&filter={SMALL_filter1:{$sf1:'America'}}&segment="ocean
另一个例子:
myfunction(space, start_date, end_date, optionalArgument4)
应返回:
[1] https://myapi.com/getData?hello&space={s:'earth'}&period={M:{start:'2018-10'end:'2018-11'}}&segment="ocean
我陷入了困境……您对此有什么管理办法?谢谢您的帮助
答案 0 :(得分:1)
首先,您错过了“&date_start =”之后的逗号。 然后,我想您可以检查该函数接收了多少个可选选项,然后将它们全部与过滤器字符串一起粘贴。为此,必须将NULL作为可选参数的默认值。
argument1 <- "2018-11"
argument2 <- "2018-12"
optionalArgument1 <- "my_first_filter"
optionalArgument2 <- "my_second_filter"
optionalArgument3 <- "my_third_filter"
myfunction <- function (argument1, argument2, optionalArgument1 = NULL, optionalArgument2 = NULL, optionalArgument3 =NULL) {
optional_options = c(optionalArgument1,optionalArgument2,optionalArgument3)
optional_request = paste0("&filter=",optional_options,collapse = "")
api_request <- paste0("myapiurl.com/getData?",
"&date_start=",argument1,
"&date_end=",argument2,optional_request)
return(api_request)
}
myfunction(argument1,argument2, optionalArgument1,optionalArgument2)
此代码并仅使用两个可选参数将产生以下输出:
> myfunction(argument1,argument2, optionalArgument1,optionalArgument2)
[1] "myapiurl.com/getData?&date_start=2018-11&date_end=2018-12&filter=my_first_filter&filter=my_second_filter"
我希望这会有所帮助。
更新: 在您提出新请求后,我已经编写了此代码,但是如果您想使用我之前所说的话,我想您应该可以这样做。
space <- "earth"
start_date <- "2018-10"
end_date <- "2018-11"
optionalArgument1 <- "America"
optionalArgument2 <- "people"
optionalArgument3 <- "size"
optionalArgument4 <- "ocean"
myfunction <- function(space, start_date, end_date, optionalArgument1=NULL, optionalArgument2=NULL, optionalArgument3=NULL, optionalArgument4=NULL){
optional_options = c(optionalArgument1,optionalArgument2,optionalArgument3)
optional_request=NULL
if(length(optional_options>0)){
optional_request = paste("&filter=",paste0("{SMALL_filter1:{$sf1:'",optional_options,"}",collapse = ""),collapse = "",sep = "")
}
if(!is.null(optionalArgument4)){
optional_request = paste(optional_request,"&filter={",optionalArgument4,"}",sep="",collapse = "")
}
api_request <- paste0("https://myapi.com/getData?hello",
#NEEDED
"&space={s:'", space,"'}",
"&period={M:{start:'",start_date,"',end:'",end_date,"'}}",optional_request)
return(api_request)
}
和输出:
> myfunction(space, start_date, end_date, optionalArgument1, optionalArgument4=optionalArgument4)
[1] "https://myapi.com/getData?hello&space={s:'earth'}&period={M:{start:'2018-10',end:'2018-11'}}&filter={SMALL_filter1:{$sf1:'America}&filter={ocean}"
> myfunction(space, start_date, end_date, optionalArgument4=optionalArgument4)
[1] "https://myapi.com/getData?hello&space={s:'earth'}&period={M:{start:'2018-10',end:'2018-11'}}&filter={ocean}"
请注意,您必须命名参数4,因为必须将其单独对待,因此不能由参数顺序使用。
答案 1 :(得分:1)
您可以这样做:
myfunction <- function(space, start_date, end_date, ..., optionalArgument4 = NULL){
args <- c(...)
filters <- paste(unlist(lapply(seq_along(args),
function(i) sprintf("SMALL_filter%s:{$sf%s:'%s'}",
i, i, args[i]))),
collapse = ",")
api_request <- paste0("https://myapi.com/getData?hello",
#NEEDED
"&space={s:'", space,"'}",
"&period={M:{start:'",start_date,"',end:'",end_date,"'}}",
#OPTIONAL
ifelse(filters=="","",sprintf("&filter={%s}",filters)),
ifelse(!is.null(optionalArgument4),
paste0("&segment=",optionalArgument4),"")
)
return(api_request)
}
调用该函数时,您将不得不命名optionalArgument4
:
space <- "earth"
start_date <- "2018-10"
end_date <- "2018-11"
optionalArgument1 <- "America"
optionalArgument2 <- "people"
optionalArgument3 <- "size"
optionalArgument4 <- "ocean"
myfunction(space, start_date, end_date, optionalArgument1, optionalArgument4 = optionalArgument4)
# [1] "https://myapi.com/getData?hello&space={s:'earth'}&period={M:{start:'2018-10',end:'2018-11'}}&filter={SMALL_filter1:{$sf1:'America'}}&segment=ocean"