multiple group_by in shiny app

落爺英雄遲暮 提交于 2021-01-18 06:48:30

问题


I have a shiny app that takes a dataframe, and applies group_by from dplyr. I can make it accept a single group, but I would like the selectInput to accept multiple grouping variables.

I can get around this problem by adding another selectInput, and then passing that to the group_by statement, but I'd like this to extend to an arbitrary number of variables. Therefore I need the single selectInput to accept multiple arguments.

Just adding multiple = TRUE does not pass the variables in a way that group_by understands, and this answer I was unable to adapt now that group_by_ is deprecated

Note,

The full version of this app uses fileInput, rather than a hardcoded dataset, hence the calls to renderUI, and reactive

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:


The answer for this, as Renu pointed out in the comments, was to replace !! with !!!, and sym with syms (this allows group_by to accept a list of variables, rather than a single variable.

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)


来源:https://stackoverflow.com/questions/50092344/multiple-group-by-in-shiny-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!