vendredi 11 février 2022

How to check if a checkbox has been selected using ShinyDashboard?

I have created an app using ShinyDashboard that has one checkboxInput. If you click on it, you will see two checkboxGroupInput where you can select 1 or 2 choices.

The idea is that if you click 1 option, you will subset your dataframe with your individual choice, or if you click both options, you will subset your dataframe with those two options.

IMAGE 1

However, I am having problems to verify which option of the checkboxGroupInput the user has selected.

Here is a reproducible example. As you can see, if you select both, you end in the first if statement, subsetting two columns. However, if you select one option (setosa, for example), you are still subsetting by the two, because it doesn't recognise the choice that you have selected. However, if you select "virginica" it is subsetted.

Moreover, it appears this warning all the time.

Warning in if (c("setosa", "virginica") %in% input$species_choice) { : the condition has length > 1 and only the first element will be used

The code:

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  
  dashboardHeader(title = "Dashboard"),
  ## Sidebar content
  dashboardSidebar(
    sidebarMenu(
      menuItem("App1", tabName = "App1", icon = icon("th"))
      
    )
  ),
  
  dashboardBody(
    fluidRow(
      tabItems(
        
        tabItem(tabName = "App1",
                sidebarPanel(
                  checkboxInput(inputId = "species", label = "Select species"),
                  
                  conditionalPanel(
                    condition = "input.species",
                    style = "margin-left: 20px;",
                  
                    checkboxGroupInput("species_choice", "Choose the species:",
                                       choices = c("setosa", "virginica"), selected = c("setosa", "virginica"))),
                ),
                mainPanel(
                  dataTableOutput("table")
                  
                )
        )
      )
    )
  )
)

server <- function(input, output, session) {
  
  mytables <- reactive({
   if(input$species){
     
     df_setosa <- iris[iris$Species=="setosa",]
     df_virginica <- iris[iris$Species=="virginica",]
     df_both <- rbind(df_setosa, df_virginica)
     
     if(c("setosa", "virginica") %in% input$species_choice){
       print("both")
       return(df_both)
     }
     
     if("setosa" %in% input$species_choice){
       print("setosa")
       return(df_setosa)
     }
     
     if("virginica" %in% input$species_choice){
       print("virginica")
       return(df_virginica)
     }
   }
  })
  
  output$table <- renderDataTable({
    mytables()
  })
  
}

shinyApp(ui, server)

I have tried this way too, but it doesn't work:

if(input$species_choice == "setosa"){
        print("setosa")
        return(df_setosa)
      }
      
      if(input$species_choice == "virginica"){
        print("virginica")
        return(df_virginica)
      }
      
      if(input$species_choice == c("setosa, virginica"){
        print("both")
        return(df_both)
      }

Does anyone know how to help me, please?

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire