I have been trying to understand tidy eval or how to use variables within tidyverse for a while, but I never seem to fully grasp it.
For example, I am trying to use ggplot with variable mappings. This would the base R version:
library(ggplot2)
var1 = "wt"
var2 = "mpg"
ggplot(mtcars, aes(x = get(var1), y = get(var2))) + geom_point()
However, based on all the documentation and discussion I have seen, the "proper" quasiquotation way would be:
ggplot(mtcars, aes(x = !!sym(var1), y = !!sym(var2))) + geom_point()
Maybe that is more comparable to:
ggplot(mtcars, aes(x = !!as.symbol(var1), y = !!as.symbol(var2))) + geom_point()
The get()
method is shorter and more readable to me. Why is it shunned by the tidyverse community?
If the data frame contains a var1
or var2
column, those will be picked up by get()
instead of the objects in your environment.
Also you'll get better auto-labelling of the expression with quasiquotation, as you're modifying the captured expression directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With