nse
Why is enquo + !! preferable to substitute + eval
I want to give an answer that is independent of dplyr, because there is a very clear advantage to using enquo over substitute. Both look in the calling environment of a function to identify the expression that was given to that function. The difference is that substitute() does it only once, while !!enquo() will correctly … Read more
dplyr: How to use group_by inside a function?
For programming, group_by_ is the counterpart to group_by: library(dplyr) mytable <- function(x, …) x %>% group_by_(…) %>% summarise(n = n()) mytable(iris, “Species”) # or iris %>% mytable(“Species”) which gives: Species n 1 setosa 50 2 versicolor 50 3 virginica 50 Update At the time this was written dplyr used %.% which is what was originally … Read more
Looping over variables in ggplot
You just need to use aes_string instead of aes, like this: ggplot(data=t, aes_string(x = “w”, y = i)) + geom_line() Note that w then needs to be specified as a string, too.
How to use a variable to specify column name in ggplot
You can use aes_string: f <- function( column ) { … ggplot( rates.by.groups, aes_string(x=”name”, y=”rate”, colour= column, group=column ) ) } as long as you pass the column to the function as a string (f(“majr”) rather than f(majr)). Also note that we changed the other columns, “name” and “rate”, to be strings. If for whatever … Read more