r - 使用 embrace {{ arg }} 在 dplyr 中选择负值的问题

标签 r dplyr rlang tidyselect

我在根据变量选择负数列时遇到问题。 我发现这里报告了类似的问题: https://github.com/tidyverse/dplyr/issues/4813但提供的解决方案不起作用(请参阅下面的 repex)。 如果有人知道解决方法,将不胜感激!! 这是问题的 repex:

# Load package
library(dplyr, warn.conflicts = F)
ex_data <- tibble(a = 1, b = 2)

# example functions
## Function for checking if target was provided
## else defaults to all numeric columns
check_target <- function(target) {
  target
  target_quoted <- rlang::enquo(target)
  if (rlang::quo_is_null(target_quoted)) {
    rlang::expr(where(is.numeric))
  } else{
    rlang::expr(!!target)
  }
}
## Just soft wrappers to show how the preivews
## function could be used
my_select_embrace <- function(data, target = NULL){
  target <- check_target(target)
  data %>% 
    select({{ target }})
}
my_select_bang <- function(data, target = NULL){
  target <- check_target(target)
  data %>% 
    select(!!target)
}
# Works
ex_data %>% 
  select(-1) %>%
  invisible()
ex_data %>% 
  my_select_bang(tidyselect::vars_select_helpers$where(is.numeric)) %>%
  invisible()
ex_data %>% 
  my_select_bang() %>%
  invisible()
# Fails
ex_data %>% 
  my_select_embrace() %>%
  invisible()
#> Error: object 'is.numeric' not found
ex_data %>% 
  my_select_bang(tidyselect::vars_select_helpers$contains('a')) %>%
  invisible()
#> Error: `contains()` must be used within a *selecting* function.
#> i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.
ex_data %>% 
  my_select_embrace(tidyselect::vars_select_helpers$where(is.numeric)) %>%
  invisible()
minus_one <- -1
ex_data %>% 
  my_select_embrace(minus_one)
#> Error: Selections can't have negative values.
ex_data %>% 
  my_select_bang(minus_one)
#> Error: Selections can't have negative values.

reprex package 创建于 2021-06-21 (v2.0.0)

最佳答案

这是您要找的吗?

library(dplyr, warn.conflicts = F)
ex_data <- tibble(a = 1, b = 2)


my_select_bang <- function(data, target = NULL){
  
  target_quoted <- enquo(target)
  target_null <- rlang::quo_is_null(target_quoted)

  data %>% 
    select(if (target_null) where(is.numeric) else !!target_quoted)
}

# defaults to `where(is.numeric)`
ex_data %>% 
  my_select_bang()
#> # A tibble: 1 x 2
#>       a     b
#>   <dbl> <dbl>
#> 1     1     2

# works with tidyselect syntax as expected
ex_data %>% 
  my_select_bang(contains("a"))
#> # A tibble: 1 x 1
#>       a
#>   <dbl>
#> 1     1

reprex package 创建于 2021-06-21 (v0.3.0)

如果您不介意在正式函数中使用默认值,那么下面的方法也适用:

my_select_bang <- function(data,
                           target = tidyselect::vars_select_helpers$where(is.numeric)){
  
  data %>% 
    select(!!target)
}

如果要捕获-1在向量中并在里面使用它 dplyr::select或您自己的功能之一,您应该使用 minus_one <- expr(-1) 捕获它.然后它不会抛出错误。

minus_one <- expr(-1)

ex_data %>% 
  my_select_bang(minus_one)

#> # A tibble: 1 x 1
#>       b
#>   <dbl>
#> 1     2

reprex package 创建于 2021-06-21 (v0.3.0)

关于r - 使用 embrace {{ arg }} 在 dplyr 中选择负值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68073762/

相关文章:

r - 如何为另一列中定义的每种类型规范化一列中的值

r - 按组观察两个连续值后进行过滤

r - 用户定义的函数在 dplyr 管道中不起作用

r - 如何将未加引号的列名列表提供给 `lapply`(以便我可以将它与 `dplyr` 函数一起使用)

r - 使用 tidyeval 编程: tidyr::unite(col = !!col) 之后的 mutate 函数

r - 如何用特定值替换日期中的所有日期值?

r - 在 R 中查找独特的投资组合

r - rlang::enexpr 和 base::substitute 之间的真正区别

regex - 使用 stringr 从 R 中的文本字符串中提取一个或多个单词

r - dplyr 中的嵌套 .key 已弃用,可以使用什么代替