JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
601 stars 81 forks source link

Not able to to quoted variables #333

Closed Laurent-Smeets-GSS-Account closed 3 years ago

Laurent-Smeets-GSS-Account commented 3 years ago

I would like to use the echarts4r package inside a function. However, I am not able to get even these very basic examples working. Is something like this even possible in the current version? and if so, how?

my_var <- "Sepal.Width"

iris %>% 
  e_charts(Species) %>%
  e_bar(my_var)
chart_f <- function(x, y){
  group_var <- enquo(x)
  plot_var <- enquo(y)

  iris %>%
    filter(Species == !!group_var) %>%
    e_charts(!!group_var) %>%
    e_bar(!!plot_var)
}  
chart_f(x = "virginica", y = "Sepal.Length")
JohnCoene commented 3 years ago

echarts4r does not support this tidyeval.

library(dplyr)
library(echarts4r)

# use the escape hatch
# functions ending in _
# they accept strings
chart_f_ <- function(y, size){
  iris %>%
    e_charts_("Petal.Width") %>%
    e_scatter_(y, size, symbol_size = 5)
}

# run
chart_f_("Sepal.Width", "Petal.Length")

# use rlang if you want to have to use bare names
chart_f <- function(y, size){
  y_label <- rlang::as_label(rlang::enquo(y))
  size_label <- rlang::as_label(rlang::enquo(size))

  iris %>%
    e_charts(Petal.Width) %>%
    e_scatter_(y_label, size_label, symbol_size = 5)
}

# run
chart_f(Sepal.Width, Petal.Length)

Let me know if this helps.

Laurent-Smeets-GSS-Account commented 3 years ago

Thanks, this is perfect. Also great thanks for the amazing package.