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)