动态命名的点传递给Lapply

发布于 2025-01-27 07:14:57 字数 913 浏览 3 评论 0原文

尽管很容易说明这种方式,但这并不是真正的HTTR2特定问题。如果我有一个与我想要lapply on的函数以及> ...的组件需要命名的函数,那么如何.. ..这样吗?我希望该函数以参数名称(IE param下面的参数)使用,这些函数是点名称,其值是lapply off的值。

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

This isn't really a httr2 specific problem though it is easy to illustrate this way. If I have a param that is being to a function that I want to lapply on and that function and the componets of ... need to named, how do I.... do that? I want the function to take the argument name (i.e. param below) use that are the dots name with the values of the vector being lapply over.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2025-02-03 07:14:57

这看起来像是有效的(从下面的评论中进行了编辑):

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)) 
}  

返回:

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

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

This looks like it works (edited from comment below):

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)) 
}  

which 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
江心雾 2025-02-03 07:14:57

这是一个使用base 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)

Here's a version that uses the base do.call function to build the call to the function with the parameter name you want

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))))
}
   

It handles all cases like

other_param <- c("x", "y")
my_func(req, other_param)
my_func(req, other_param=1:2)
my_func(req, other_param=other_param)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文