无法获得函数 'enquo' 在闪亮模块中使用反应变量 + dplyr 动词

Unable to get function 'enquo' working with reactive variables + dplyr verbs in shiny modules

我正在尝试根据一个或多个反应变量对输入数据进行分组(Cal.Month区域 , SalesTeam) 并输出绘制数据的线图 & table;基于 programming with dplyr 上的博客 post,我使用了“!!”和函数 enquo() 来取消引用传递给 group_by dplyr 动词的反应变量,如下所示:

TS_module <- function(input, output, session, data, 
 prod_specific_type, x, y, z, show_data) {

 TS_with_type <- reactive({

   first_group<- x()
   second_group<- y()

   x<- enquo(first_group)
   y<- enquo(second_group)

      data %>%
        filter(Prod.Specific == as.character(prod_specific_type)) %>%
        group_by(!!x, !!z) %>%
        summarize(Amount = sum(Amount), Qty = sum(Qty))
    })

我得到一个错误:找不到对象 'Region'。

看来,对象 'Region'(以及用于 group_by 的其他变量)被读取为 "Region"(带双引号),而函数 enquo() 应该已经解析并将该值作为 quosure(即区域)传递。

我将参数传递给函数 enquo() 的方式或任何其他解决此问题的方法是否存在问题?

谢谢

这是整个模块 UI/Server 代码:

TS_module_UI <- function(id) {
ns <- NS(id)

   tagList(
   plotOutput(ns("lineplot")),
   dataTableOutput(ns("teamsynctable"))
  )
 }

 TS_module <- function(input, output, session, data, 
 prod_specific_type, x, y, z, show_data) {

 TS_with_type <- reactive({
      first_group<- x()
      second_group<- y()

      x<- enquo(first_group)
      y<- enquo(second_group)

      data %>%
        filter(Prod.Specific == as.character(prod_specific_type)) %>%
        group_by(!!x, !!z) %>%
        summarize(Amount = sum(Amount), Qty = sum(Qty))
    })

 output$lineplot <- renderPlot({
   ggplot(data = TS_with_type(), aes_string(x = x(), y = y())) +
   geom_line()   
 })

app端的UI/server代码:

sidebarLayout(


  sidebarPanel(

  selectInput(inputId = "y", 
              label = "Choose Qty or $$Amt:",
              choices = c("Quantity" = "Qty", 
                          "$$ Amt" = "Amount"), 
              selected = "Qty"),

  selectInput(inputId = "x", 
              label = "Time-Frame:",
              choices = c("Monthly" = "Cal.Month", 
                          "Yearly" = "Year"), 
              selected = "Cal.Month"),

  selectInput(inputId = "z", 
              label = "Group by:",
              choices = c("Region" = "Region",
                          "Industry" = "Industry", 
                          "Sales Team" = "SalesTeam"),
              selected = "Region"),
  ))

 server <- function(input, output, session) {

 x     <- reactive(input$x)
 y     <- reactive(input$y)
 z     <- reactive(input$z)
 show_data <- reactive(input$show_data)


 callModule(TS_module, "new", data = ts_data, prod_specific_type = "A", x, y, z, show_data)
 callModule(TS_module, "add", data = ts_data, prod_specific_type = "B", x, y, z, show_data)
 callModule(TS_module, "renew", data = ts_data, prod_specific_type = "C", x, y, z, show_data)

  }

enquo 在这里不起作用,因为 dplyr 需要一个列名(它又是一个 R 对象,而不仅仅是一个字符串)

evalparse 是您所需要的。

修改后的代码:

TS_module <- function(input, output, session, data, 
 prod_specific_type, x, y, z, show_data) {

 TS_with_type <- reactive({

   first_group<- x()
   second_group<- y()


      data %>%
        filter(Prod.Specific == as.character(prod_specific_type)) %>%
        group_by_(first_group, second_group) %>%
        #group_by(!!eval(parse(text=first_group)), !!eval(parse(text=second_group))) %>%
        summarize(Amount = sum(Amount), Qty = sum(Qty))
    })

编辑:

在最新的解决方案中使用了group_by_,非常简单。抱歉之前的误导。