使用 dplyr 编程时出现问题 - 错误提示在管道的一部分中找不到对象但工作较早
Problem programming with dplyr - error saying object not found in one part of pipe but working earlier
我有一个工作函数,if else
大部分是重复代码,所以我试图通过使用内联 if else 语句来消除重复。令我感到奇怪的是,同一个片段在代码的一个地方有效,但在另一个地方却不行。
library(dplyr)
library(highcharter)
plot_highchart <- function(.data,
group_by_variable = TRUE,
x_value = "Year",
y_value = "total",
.group = service,
.stacking = "normal",
chart_type = "column") {
.data %>%
# this next line works. If you comment out the hchart part it will group by and summarize
group_by(Year, if (group_by_variable == TRUE) !!rlang::enquo(.group) else NULL) %>%
summarize(total = sum(Spending)) %>%
hchart(chart_type, hcaes(x = !!rlang::ensym(x_value),
y = !!rlang::ensym(y_value),
group = if (group_by_variable == TRUE) !!rlang::ensym(.group) else NULL))
# same bit as before but I get an error
}
这是我在尝试 运行 时遇到的错误:
Error: Problem with `mutate()` input `group`.
x object 'group_by_variable' not found
i Input `group` is `if (group_by_variable == TRUE) service else NULL`.
我觉得很奇怪,因为之前发现了 group_by_variable
。不太确定从这里去哪里。
这是数据输入:
structure(list(Year = c(2016, 2016, 2016, 2016, 2016, 2016),
service = structure(c(10L, 10L, 10L, 10L, 10L, 10L), .Label = c("Defense Logistics Agency",
"Chemical and Biological Defense Program", "Defense Information Systems Agency",
"United States Special Operations Command", "Office of the Secretary Of Defense",
"Missile Defense Agency", "Defense Advanced Research Projects Agency",
"Navy", "Army", "Air Force"), class = "factor"), Spending = c(0.803,
0.628, 0.2, 23.72, 4.782, 12.152)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
hcaes()
捕获您提供给 group
的表达式并延迟其计算。但是,表达式在 highcharter 包中发生了一系列变化。这些步骤之一 assigns the global environment to be the evaluation context,然后导致 R 解释器在全局范围内查找 group_by_variable
,而不是在定义它的函数中查找。
一种解决方法是将 if
语句拉到 haes()
之外,这样 group_by_variable
就不会被函数捕获为要计算的表达式的一部分:
plot_highchart <- function(.data,
group_by_variable = TRUE,
x_value = "Year",
y_value = "total",
.group = service,
.stacking = "normal",
chart_type = "column") {
g <- if (group_by_variable == TRUE) list(group = rlang::ensym(.group))
else NULL
.data %>%
group_by(Year, !!g[[1]]) %>%
summarize(total = sum(Spending)) %>%
hchart(chart_type, hcaes(x = !!rlang::ensym(x_value),
y = !!rlang::ensym(y_value),
!!!g))
}
plot_highchart( .data ) # Works
plot_highchart( .data, group_by_variable=FALSE ) # Also works
在这里,我还将分组符号存储在命名列表中,然后将该列表与 !!!
一起使用。这是处理 group_by_variable == FALSE
案例所必需的,因为:
hcaes( x = ..., y = ... ) # Works
hcaes( x = ..., y = ..., group = NULL ) # Doesn't
我有一个工作函数,if else
大部分是重复代码,所以我试图通过使用内联 if else 语句来消除重复。令我感到奇怪的是,同一个片段在代码的一个地方有效,但在另一个地方却不行。
library(dplyr)
library(highcharter)
plot_highchart <- function(.data,
group_by_variable = TRUE,
x_value = "Year",
y_value = "total",
.group = service,
.stacking = "normal",
chart_type = "column") {
.data %>%
# this next line works. If you comment out the hchart part it will group by and summarize
group_by(Year, if (group_by_variable == TRUE) !!rlang::enquo(.group) else NULL) %>%
summarize(total = sum(Spending)) %>%
hchart(chart_type, hcaes(x = !!rlang::ensym(x_value),
y = !!rlang::ensym(y_value),
group = if (group_by_variable == TRUE) !!rlang::ensym(.group) else NULL))
# same bit as before but I get an error
}
这是我在尝试 运行 时遇到的错误:
Error: Problem with `mutate()` input `group`.
x object 'group_by_variable' not found
i Input `group` is `if (group_by_variable == TRUE) service else NULL`.
我觉得很奇怪,因为之前发现了 group_by_variable
。不太确定从这里去哪里。
这是数据输入:
structure(list(Year = c(2016, 2016, 2016, 2016, 2016, 2016),
service = structure(c(10L, 10L, 10L, 10L, 10L, 10L), .Label = c("Defense Logistics Agency",
"Chemical and Biological Defense Program", "Defense Information Systems Agency",
"United States Special Operations Command", "Office of the Secretary Of Defense",
"Missile Defense Agency", "Defense Advanced Research Projects Agency",
"Navy", "Army", "Air Force"), class = "factor"), Spending = c(0.803,
0.628, 0.2, 23.72, 4.782, 12.152)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
hcaes()
捕获您提供给 group
的表达式并延迟其计算。但是,表达式在 highcharter 包中发生了一系列变化。这些步骤之一 assigns the global environment to be the evaluation context,然后导致 R 解释器在全局范围内查找 group_by_variable
,而不是在定义它的函数中查找。
一种解决方法是将 if
语句拉到 haes()
之外,这样 group_by_variable
就不会被函数捕获为要计算的表达式的一部分:
plot_highchart <- function(.data,
group_by_variable = TRUE,
x_value = "Year",
y_value = "total",
.group = service,
.stacking = "normal",
chart_type = "column") {
g <- if (group_by_variable == TRUE) list(group = rlang::ensym(.group))
else NULL
.data %>%
group_by(Year, !!g[[1]]) %>%
summarize(total = sum(Spending)) %>%
hchart(chart_type, hcaes(x = !!rlang::ensym(x_value),
y = !!rlang::ensym(y_value),
!!!g))
}
plot_highchart( .data ) # Works
plot_highchart( .data, group_by_variable=FALSE ) # Also works
在这里,我还将分组符号存储在命名列表中,然后将该列表与 !!!
一起使用。这是处理 group_by_variable == FALSE
案例所必需的,因为:
hcaes( x = ..., y = ... ) # Works
hcaes( x = ..., y = ..., group = NULL ) # Doesn't