闪亮应用程序中的多个group_by

Con*_*son 5 r shiny dplyr

我有一个闪亮的应用程序,它需要一个数据帧,并group_bydplyr. 我可以让它接受单个组,但我希望它selectInput接受多个分组变量。

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

仅添加multiple = TRUE不会以可以理解的方式传递变量group_by,并且我现在无法适应这个答案group_by_,该答案已被弃用

笔记,

此应用程序的完整版本使用fileInput,而不是硬编码数据集,因此调用renderUI、 和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)
Run Code Online (Sandbox Code Playgroud)

Con*_*son 5

正如 Renu 在评论中指出的那样,答案是替换为, 和with !!(这允许接受变量列表,而不是单个变量。!!!symsymsgroup_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)
Run Code Online (Sandbox Code Playgroud)