我们需要使用
enquo()
&
quo_name()
连同
hcaes_string
-
enquo()
捕获用户作为参数提供的表达式(&P);返回一个quosure。
-
quo\u name()
压扁quosure并将其转换为字符串。
看着哈德利解释
tidyeval
在这个
5min video
如果你没有听说过的话。更多关于
潮水期
here
library(tidyverse)
library(lubridate)
library(highcharter)
library(nycflights13)
flights_2 <- flights %>%
mutate(dep_mo = ymd(str_c(year, month, "01", sep = "-"))) %>%
group_by(dep_mo) %>%
summarize(arr_delay = mean(arr_delay, na.rm = TRUE))
h_fun2 <- function(df, x, y) {
x <- enquo(x)
y <- enquo(y)
hchart(df,
type = "line",
hcaes_string(quo_name(x), quo_name(y)),
name = "Average Arrival Delay"
)
}
h_fun2(df = flights_2, x = dep_mo, y = arr_delay)
编辑
:截至2018年4月18日,开发版本
highcharter
支架
潮水期
所以
h_fun2
可以重写为:
h_fun2 <- function(df, x, y) {
x <- enexpr(x)
y <- enexpr(y)
hchart(df,
type = "line",
hcaes(!!x, !!y),
name = "Average Arrival Delay"
)
}