I'm struggling to understand this.
The below lets me filter my data.frame in a "tidy" way, and draw a plot using plotly. In this case, I'm using plotly's formula-based API to say which columns of a data frame to use:
library(plotly)
tidy_filter = function(data, x) {
x = enquo(x)
filter(data, !!x > 5)
}
mtcars %>%
tidy_filter(wt) %>%
plot_ly(x = ~wt, y = ~wt)
I can wrap this in a single function to get the same result:
tidy_ply = function(data, x) {
x = enquo(x)
data = filter(data, !!x > 5)
plot_ly(data, x = x, y = x)
}
tidy_ply(mtcars, wt)
Now:
I assume that
enquo(x)
in this case is at least in part equivalent to~wt
since that's how it seems to work. But they are two different things (quosure VS formula). What is the relationship between them, and why does the above work?The advantage of plotly's formula API is that if I want to manipulate the input value, I can do things like
~wt/2
. But in the above, doingplot_ly(data, x = x, y = x/2)
produces an error. Is there a way to make this work?
I guess the general question is how to best combine the tidy eval approach with plotly's formula approach?