动态命名的点传递给 lapply

Dynamically named dots passed to lapply

这并不是真正的 httr2 特定问题,尽管用这种方式很容易说明。如果我有一个参数是我想要 lapply 的函数,并且该函数和 ... 的组件需要命名,我该怎么做......我希望函数采用参数名称(即下面的 param),使用点名称,向量的值超过 lapply

library(httr2)
req <- request("http://example.com")
param <- c("foo", "bar")

## hard code param (this is what i am hoping to generate)
lapply(param, \(x) req_url_query(req, param = x))
#> [[1]]
#> <httr2_request>
#> GET http://example.com?param=foo
#> Body: empty
#> 
#> [[2]]
#> <httr2_request>
#> GET http://example.com?param=bar
#> Body: empty


## want the ... to dynamically named
my_func <- function(req, ...) {
  lapply(..., \(x) req_url_query(req, ...))
}

other_param <- c("x", "y")
my_func(req, other_param)
#> Error in `modify_list()`:
#> ! All components of ... must be named

这看起来可行(根据以下评论编辑):

my_func <- function(req, ...) {   
  dots <- list(...)   
  dots_chr <- unlist(dots)
  function_string <- paste0("lapply(dots_chr, \(x) req_url_query(req, ", names(dots), "= x))")   
  eval(parse(text = function_string)) 
}  

哪个returns:

$pizza1
<httr2_request>
GET http://example.com?pizza=is_great
Body: empty

$pizza2
<httr2_request>
GET http://example.com?pizza=is_healthy
Body: empty

这是一个版本,它使用基础 do.call 函数来构建对具有您想要的参数名称的函数的调用

my_func <- function(req, ...) {
  mc <- as.list(match.call()[-(1:2)])
  stopifnot(length(mc)==1)
  pname <- deparse1(mc[[1]])
  if(!is.null(names(mc))) {
    pname[names(mc)!=""] <- names(mc)[[names(mc)!=""]]
  }
  lapply(list(...)[[1]], \(x) do.call("req_url_query", setNames(list(quote(req), x), c("", pname))))
}
   

它处理像

这样的所有情况
other_param <- c("x", "y")
my_func(req, other_param)
my_func(req, other_param=1:2)
my_func(req, other_param=other_param)