lundi 27 avril 2020

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?




Aucun commentaire:

Enregistrer un commentaire