简体   繁体   中英

RShiny: how can I summarise/subset/filter data using checkboxGroupInput?

I'm trying to use checkbox input to subset, filter, and summarise data. I've mocked up the problem with iris data.

I'm trying to allow the user to summarise iris data by sepal width/length, petal width/length, or both. Individually, each checkbox works, but using multiple input options is NOT working. Selecting both "Sepal" and "Petal" returns only Sepal data.

UI:

ui <- fluidPage(
    fluidRow(
        box(
            title = "Choose data", width = 3, solidHeader = TRUE,
            status = "primary", collapsible = TRUE,
            checkboxGroupInput("iris_select", "Select measurements:",
                        choices = list("Sepal", "Petal"),
                        selected = c("Sepal")),
            verbatimTextOutput("whatdidiselect")),

        box(
            title = "See your data output", width = 9, solidHeader = TRUE,
            status = "success", collapsible = TRUE,
            DT::dataTableOutput("iris_output")
        )))

And server:

server <- function(input, output) {

     output$whatdidiselect <- renderText(input$iris_select)

     iris_summary <- reactive({
        if(all(c("Sepal") %in% input$iris_select)){
            iris %>% 
                group_by(., Species) %>%
                summarise(Mean_Sepal_Length = mean(Sepal.Length),
                          Mean_Sepal_Width = mean(Sepal.Width))}

            if(all(c("Petal") %in% input$iris_select)){
                iris %>% 
                    group_by(., Species) %>%
                    summarise(Mean_Petal_Length = mean(Petal.Length),
                              Mean_Petal_Width = mean(Petal.Width))}

                if(all(c("Sepal", "Petal") %in% input$iris_select)){
                    iris %>% 
                        group_by(., Species) %>%
                        summarise(Mean_Sepal_Length = mean(Sepal.Length),
                                  Mean_Sepal_Width = mean(Sepal.Width),
                                  Mean_Petal_Length = mean(Petal.Length),
                                  Mean_Petal_Width = mean(Petal.Width))} 
    })

    output$iris_output <- DT::renderDataTable({
        iris_summary()})
}

This seems like it should be simple. Can someone point out where I'm going wrong?

You need to reverse the logic in all (at least for first two checks), for example:

server <- function(input, output) {

  output$whatdidiselect <- renderText(input$iris_select)

  iris_summary <- reactive({
    if(all(input$iris_select == 'Sepal')){
      iris %>% 
        group_by(., Species) %>%
        summarise(Mean_Sepal_Length = mean(Sepal.Length),
                  Mean_Sepal_Width = mean(Sepal.Width))}

    else if(all(input$iris_select == 'Petal')){
      iris %>% 
        group_by(., Species) %>%
        summarise(Mean_Petal_Length = mean(Petal.Length),
                  Mean_Petal_Width = mean(Petal.Width))}

    else if(all(input$iris_select %in% c('Sepal', 'Petal'))){
      iris %>% 
        group_by(., Species) %>%
        summarise(Mean_Sepal_Length = mean(Sepal.Length),
                  Mean_Sepal_Width = mean(Sepal.Width),
                  Mean_Petal_Length = mean(Petal.Length),
                  Mean_Petal_Width = mean(Petal.Width))} 
  })

  output$iris_output <- DT::renderDataTable({
    iris_summary()})
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM