如果我想使用`suptor_with()`'使用拥抱操作员怎么办?
使用拥抱操作员消除了将参数包含在双引号标记中的函数的必要性。
但是,如果我想将其与start_with()
一起使用该怎么办?
# This works.
test <- function(var) {
mtcars |>
dplyr::select({{ var }})
}
test(mpg) |> head()
#> mpg
#> Mazda RX4 21.0
#> Mazda RX4 Wag 21.0
#> Datsun 710 22.8
#> Hornet 4 Drive 21.4
#> Hornet Sportabout 18.7
#> Valiant 18.1
# But this won't work.
test2 <- function(var) {
mtcars |>
dplyr::select(starts_with({{ var }}))
}
test2(m) |> head()
#> Error in `dplyr::select()`:
#> ! オブジェクト 'm' がありません
Using the embracing operator eliminates the need to enclose arguments passed to a function in double quotation marks.
But what if I want to use it with starts_with()
?
# This works.
test <- function(var) {
mtcars |>
dplyr::select({{ var }})
}
test(mpg) |> head()
#> mpg
#> Mazda RX4 21.0
#> Mazda RX4 Wag 21.0
#> Datsun 710 22.8
#> Hornet 4 Drive 21.4
#> Hornet Sportabout 18.7
#> Valiant 18.1
# But this won't work.
test2 <- function(var) {
mtcars |>
dplyr::select(starts_with({{ var }}))
}
test2(m) |> head()
#> Error in `dplyr::select()`:
#> ! オブジェクト 'm' がありません
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下代码:
输出:
Try the following code:
Output:
即使
选择
采用未引用的字符串,因此可以与test(M)
一起使用,但对于starts_with
,IE并非如此Even though
select
takes unquoted strings and hence works withtest(m)
, this is not the case forstarts_with
, i.e.