动态命名的点传递给Lapply
尽管很容易说明这种方式,但这并不是真正的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像是有效的(从下面的评论中进行了编辑):
返回:
This looks like it works (edited from comment below):
which returns:
这是一个使用base
do.call
函数的版本,用您想要的参数名称构建对函数的调用,例如
Here's a version that uses the base
do.call
function to build the call to the function with the parameter name you wantIt handles all cases like