R/select_not.R
select_not.Rd
Drop columns provided as unquoted names to arguments.
select_not(data, ...)
data | The data set to work with. |
---|---|
... | Arguments as with |
Whatever select would have done with a minus sign, if that had worked as it logically should.
This function is not exported, as most should have no problems using
basic select
functionality for common needs. However, trying to
drop/deselect variables that started without quotes was a maddening
exploration of tidyeval I feel no better about myself for having gone
through. The problem is that something like following doesn't work:
select(-!!var)
😲
If you follow typical examples provided for tidy evaluation, you'll just
get `invalid argument` errors. I needed this functionality because an
operation was to be performed on some columns, but not if some operations
had been performed on others, and those others were passed as unquoted
variables via vars()
. See pre_process.
In the end, it was far easier to write a two line function than figure out the tidy eval approach that would have worked with less code. It literally just does:
not_these = names(select(data, ...))
data %>% select(-one_of(not_these))
Lionel's
answer on SO shows how one could do it. He is a
developer for RStudio, so one can assume it's a fairly spot on suggestion.
Yet even that would need a bit of modification to work with a
vars()
, which I needed for multiple column entries to a single
argument. Here is what ultimately would work with tidyeval using that
approach.
x = vars_input %>% map(function(sym) call("-", sym))
data %>% select(!!!x)
You then would need an 'x
' for every argument's input. And no one reading the
code would know what's going on, which goes against one of the goals for this
package. 😞
Here is another solution that I came across later that speaks directly to the problem, but is essentially the same solution.