I'm creating a package that uses non-standard evaluation to keep track of the meaning of columns. The package passes a data frame among functions, which do various things do the same set of columns. Nonstandard evaluation works great for this:
my_select <- function(df, xcol, ycol) {
new_df <- dplyr::select(df, !!xcol, !!ycol)
new_df
}
my_select(mtcars, quo(wt), quo(mpg))
However, I'd like a function that works with a formula:
my_lm <- function(df, xcol, ycol) {
new_lm <- lm(!!xcol, !!ycol, data=df)
new_lm
}
my_lm(mtcars, quo(wt), quo(mpg)
returns Error in !xcol : invalid argument type
. I've tried all sorts of combinations of quo()
, enquo()
, and !!
, but the basic problem is that I don't know what kind of object lm
needs.