多个group_by在闪亮的应用程序中

时间:2018-04-29 23:42:00

标签: r shiny dplyr

我有一个闪亮的应用程序,它采用数据框,并从group_by应用dplyr。我可以让它接受一个组,但我希望selectInput接受多个分组变量。

我可以通过添加另一个selectInput来解决此问题,然后将其传递给group_by语句,但我希望将其扩展为任意数量的变量。因此,我需要单个selectInput来接受多个参数。

仅添加multiple = TRUE不会以group_by理解的方式传递变量,而this answer我现在无法适应group_by_已被弃用

注意,

此应用的完整版使用fileInput,而非硬编码数据集,因此调用renderUIreactive

library(shiny)
library(DT)
library(dplyr)

ui <- fluidPage(

  titlePanel("app"),

  sidebarLayout(
    sidebarPanel(

      uiOutput("groups")

    ),


    mainPanel(

      DT::dataTableOutput("summary")
    )
  )
)


server <- function(input, output) {
  mydata <- reactive({structure(list(School = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                                          2L, 1L, 1L, 2L, 2L), .Label = c("School1", "School2"), class = "factor"), 
                                     Town = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 
                                                        2L, 1L), .Label = c("Levin", "Wellington"), class = "factor"), 
                                     Income = c(6314L, 3546L, 3541L, 846684L, 231123L, 564564L, 
                                                545L, 1325L, 484L, 51353L, 465546L, 564546L)), .Names = c("School", 
                                                                                                          "Town", "Income"), class = "data.frame", row.names = c(NA, -12L
                                                                                                          ))})



  output$groups <- renderUI({
    df <- mydata()
    selectInput(inputId = "grouper", label = "Group variable", choices = names(df), multiple = TRUE)
  })




  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!rlang::sym(input$grouper)) %>%
      dplyr::summarise(mean_income = mean(Income), na.rm = TRUE)
  })

  output$summary <- DT::renderDataTable({
    DT::datatable(summary_data())
  })



}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

正如Renu在评论中指出的那样,答案就是将!!替换为!!!,将sym替换为syms(这允许group_by接受变量列表,而不是单个变量。

library(shiny)
library(DT)
library(dplyr)

ui <- fluidPage(

  titlePanel("app"),

  sidebarLayout(
    sidebarPanel(

      uiOutput("groups")

    ),


    mainPanel(

      DT::dataTableOutput("summary")
    )
  )
)


server <- function(input, output) {
  mydata <- reactive({structure(list(School = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                                          2L, 1L, 1L, 2L, 2L), .Label = c("School1", "School2"), class = "factor"), 
                                     Town = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 
                                                        2L, 1L), .Label = c("Levin", "Wellington"), class = "factor"), 
                                     Income = c(6314L, 3546L, 3541L, 846684L, 231123L, 564564L, 
                                                545L, 1325L, 484L, 51353L, 465546L, 564546L)), .Names = c("School", 
                                                                                                          "Town", "Income"), class = "data.frame", row.names = c(NA, -12L
                                                                                                          ))})



  output$groups <- renderUI({
    df <- mydata()
    selectInput(inputId = "grouper", label = "Group variable", choices = names(df), multiple = TRUE)
  })




  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(mean_income = mean(Income), na.rm = TRUE)
  })

  output$summary <- DT::renderDataTable({
    DT::datatable(summary_data())
  })



}

shinyApp(ui, server)