Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tidy eval vs base or get() vs sym() vs as.symbol()

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?

like image 533
burger Avatar asked Apr 30 '19 22:04

burger


1 Answers

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.

like image 73
Lionel Henry Avatar answered Oct 09 '22 14:10

Lionel Henry