4

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)

Baraliuh
  • 2,009
  • 5
  • 11
  • 1
    Try `ex_data %>% select(-all_of(-minus_one))` – akrun Jun 21 '21 at 19:41
  • 1
    Oh yeah, it might not always be negative, and sometimes it is a `tidyselect` (e.g., `where`) expression captured by `rlang::expr' so that would give me other issues. I guess my example was a bit oversimplified. – Baraliuh Jun 21 '21 at 19:43
  • 1
    Maybe also `ex_data %>% select(!!rlang::parse_expr(glue::glue("{str_remove(sign(minus_one), '\\\\d+')}all_of({abs(minus_one)})")))` – akrun Jun 21 '21 at 20:00
  • Yeah, I have updated my question a bit to give it a bit more depth to my generic problem. – Baraliuh Jun 21 '21 at 20:01
  • 1
    In your update, the `tidyselect::vars_select_helpers$where(is.numeric)` returns a function while the second one, needs a data as `tidyselect::vars_select_helpers$contains('a')` fails standalone – akrun Jun 21 '21 at 20:11
  • 1
    Ok, I have made a few examples now... I guess the issue I am really trying to solve is to capture a `tidyselect`, string, integer, etc., to use inside `tidyverse` functions that default to `where(is.numeric)` if NULL is provided (the default setting). Guess I am still struggling with understanding how to properly quote and unquote expressions. I just noticed that `contains` also did not work. – Baraliuh Jun 21 '21 at 20:18

2 Answers2

1

Is this what you are looking for?

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

Created on 2021-06-21 by the reprex package (v0.3.0)

If you don't mind having the default value inside the functions formals, then the approach below works as well:

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

If you want to capture -1 in a vector and use it inside dplyr::select or one of your own functions you should capture it with minus_one <- expr(-1). Then it won't throw an error.

minus_one <- expr(-1)

ex_data %>% 
  my_select_bang(minus_one)

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

Created on 2021-06-21 by the reprex package (v0.3.0)

TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
  • 1
    Since I do the operation `check_target` in a lot of places and use the output even more so, I would prefer to have it as an isolated function rather than having an `if` statement each time. I also do not need `-1` to be saved as a variable, it was just to examplify I guess. Though I did realize when I was walking my dog that I have to quote inside the `my_select` function. I will accept this answer and also post my solution. – Baraliuh Jun 21 '21 at 21:17
0

One way to make the original design work is to quot target before sending it to check_target and using bang-bang (!!) instead of embrace {{ arg }}:

# 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) {
  if (rlang::quo_is_null(target)) {
    rlang::expr(where(is.numeric))
  } else{
    target
  }
}
## Just soft wrapper to show how the preivews
## function could be used
my_select_bang <- function(data, target = NULL){
  target <- rlang::enquo(target)
  target <- check_target(target)
  data %>% 
    select(!!target)
}
# Works
ex_data %>% 
  select(-1) %>%
  invisible()
ex_data %>% 
  select(contains('a')) %>%
  invisible()
ex_data %>% 
  my_select_bang(where(is.numeric)) %>%
  invisible()
ex_data %>% 
  my_select_bang() %>%
  invisible()
ex_data %>% 
  my_select_bang(contains('a')) %>%
  invisible()
ex_data %>% 
  my_select_bang(-1) %>%
  invisible()

Created on 2021-06-21 by the reprex package (v2.0.0)

Baraliuh
  • 2,009
  • 5
  • 11