I have an issue with selecting negative columns based on a variable.
I found a similar issue reported here:
https://github.com/tidyverse/dplyr/issues/4813 but the provided solution does not work (see repex
below).
If anyone knows a workaround, that would be very appreciated!!
Here is a repex
of the issue:
# 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.
Created on 2021-06-21 by the reprex package (v2.0.0)