Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dplyr进行编程时出现问题-错误是在管道的某个部分中找不到对象,但可以更早地工作_R_Dplyr_Rlang_R Highcharter - Fatal编程技术网

使用dplyr进行编程时出现问题-错误是在管道的某个部分中找不到对象,但可以更早地工作

使用dplyr进行编程时出现问题-错误是在管道的某个部分中找不到对象,但可以更早地工作,r,dplyr,rlang,r-highcharter,R,Dplyr,Rlang,R Highcharter,我有一个与andif-else一起工作的函数,该函数在很大程度上是重复的代码,因此我试图通过使用一个in-line if-else语句来消除重复。对我来说奇怪的是,同一个代码片段在代码的一个地方工作,但在另一个地方却不工作 library(dplyr) library(highcharter) plot_highchart <- function(.data, group_by_variable = TRUE,

我有一个与and
if-else
一起工作的函数,该函数在很大程度上是重复的代码,因此我试图通过使用一个in-line 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

}
我觉得这很奇怪,因为以前就发现了
groupby\u variable
。我真的不知道从这里到哪里去

以下是数据的dput:

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软件包中,该表达经历了一系列变化。这些步骤中的一个,这将导致R解释器在全局范围内查找
groupby\u variable
,而不是在定义它的函数中

一种解决方法是将
if
语句拉到
haes()
之外,这样
group\u by\u变量
就不会被函数捕获为要计算的表达式的一部分:

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

嗨,本。这个问题实际上和你的另一个SO问题是一样的。当
mutate\u mapping
调用
parse\u quo
时,它将表达式分配给全局环境,在全局环境中它将查找
group\u by\u variable
。我可以向您展示一种解决方法,但您能否澄清当
group\u by\u variable==FALSE时您想要做什么?您不能将
group=NULL
传递给
haes()
,因为它在某个点显式引用
group
。尝试
hchart(.data,“column”,hcaes(x=年份,y=支出,group=NULL));rlang::last_error()
查看位置。当
group_by_variable==FALSE时,我只希望它按年份分组,然后在
hcaes
中不使用group参数,谢谢。奇怪的是,如果我把这个写出来(没有函数),然后执行
group=NA
,这将起作用,但如果不是这样,就不能作为行中的一部分
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
hcaes( x = ..., y = ... )                 # Works
hcaes( x = ..., y = ..., group = NULL )   # Doesn't