I'm trying to wrap my head around the different quo/unquo syntaxes and when each should be used.
I am mostly writing functions that pass a dataframe and columns to use as argument -- to plot using ggplot or summarize/manipulate data with dplyr (group_by, summarize, mutate ect). However, on occasion I also have to use a function that does not use NSE within my overall function.
From what I have read, my understanding is that:
1) if I'm referencing a column in a dataframe then I don't need to capture the environment and I can use ensym
or sym
. Is this correct? Would there be an issue using enquo
, or it is just not necessary?
2) if I use ensym
that the user could technically enter both a string or bare column name in the argument.
Based on this my typical function setup would look something like this:
library(tidyverse)
dataset <- mtcars
myfun <- function(dat, xvar, yvar, group){
#either manipulate data
x <- dat %>% group_by(!!ensym(group)) %>%
mutate(new = !!ensym(xvar)*5) %>%
summarize(medianx=median(!!ensym(xvar), na.rm=TRUE),
median_new=median(new, na.rm=TRUE))
#or plot data
p <- ggplot(dat, aes(x=!!ensym(xvar), y=!!ensym(yvar))) +
geom_point()
#sometime require referencing the column with NSE function..
median(dat[[xvar]]) #works if require string in argument
#how would you reference this with bare argument column? Convert ensym to string?
median(dat[[?????]])
}
#both work with ensym, only the later with sym
myfun(dataset, xvar=mpg, yvar=disp, group=cyl)
myfun(dataset, xvar="mpg", yvar="disp", group="cyl")
How would one convert the bare column argument or symbol to a string for use in the last line of myfun above? I tried rlang::as_string(!!ensym(xvar))
but it doesn't work.