Question
How would I do the following using quosures?
library(tidyverse)
lkp <- c("am", "vs", "Sepal.Width", "Sepal.Length")
stringSelect <- function(mdat) {
lkpOK <- intersect(lkp, names(mdat))
mdat %>% select(one_of(lkpOK))
}
stringSelect(mtcars)
stringSelect(iris)
So basically, how can I subset lkpq
below to achieve the same thing with quos
to avoid the warning?
lkpq <- quos(am, vs, Sepal.Width, Sepal.Length)
quosSelect <- function(mdat) {
lkpqOK <- lkpq ##???
mdat %>% select(!!!lkpqOK)
}
quosSelect(mtcars) ## does not work
quosSelect(iris)
Context
In my application I am mixing traditional R selectors and tidyverse
verbs but I want to stick to one system for consistency. Most of the things are easy to translate from one "universe" to another but I am struggling with this bit.
Footnote
I know that there are plenty possibilities to solve that. But for the sake of my learning let's assume that lkpq
is fixed and cannot be changed. So I am really interested to learn how I could use a list of quosures with select
if some of the elements are not part of the underlying data.