I'd like to exclude columns of a tibble. I got an exclude expression: a mix of possible column names and tidyselect expressions. This is what I tried:
library(tidyverse)
library(rlang)
# my vector of columns and tidyselect expressions
exclude_expression <- c('-name', '-ends_with("_x")', '-id')
# dummy dataframe
# note: column "id" does not exist in the tibble
dat <-
tribble(
~name, ~coord_x, ~coord_y,
"ben", "1", "2",
"anna", "3", "4"
)
# select statement where columns should be excluded, if they are present
dat %>%
select(
!!!parse_exprs(exclude_expression)
)
#> Error: Can't subset columns that don't exist.
#> x Column `id` doesn't exist.
Created on 2022-06-03 by the reprex package (v2.0.0)
Importantly, the pipe should not fail, if a column does not exist (in contrast to my example). The expected output for my example is:
# A tibble: 2 x 1
coord_y
<chr>
1 2
2 4