How to gray out a selectInput when the checkbox is checked?

Hi all

I am a while buzzy with making a Shiny

I visualize the data in a ggplot, there are some options (dropdown menu and the checkbox.)

Via a if loop, the checkbox is prioritized above the dropdown menu, it would be more clear if the dropdown menu is greyed-out/disable when the checkbox is checked and re-abled when the checkbox is de-checked.

This is the UI chunk:

tabPanel("Distribution based on SYSTEM", icon = icon("fa-duotone fa-microscope", verify_fa = FALSE),
                                    sidebarLayout(
                                      sidebarPanel(
                                        checkboxInput("all_cobas", label = "Select all and only Cobas", value = FALSE),
                                        selectInput("system", "System:", choices = NULL)),
                                      mainPanel(
                                        plotOutput("barplot3")))),

This is the server chunck:

 updateSelectizeInput(session, 'system', choices = df1$InstrumentName, server = TRUE)
    
    output$barplot3 <- renderPlot({
      if (input$system!=""){
        
        if(input$all_cobas == TRUE) {
          df1_filtered <- df1[grep("cobas", df1$InstrumentName), ]
        }
        else {
          df1_filtered <- df1[df1$InstrumentName == input$system,]
        }
        
        #Calculate the amount of Result-samples each hour:
        hours_set <- hms(df1_filtered$ResultTime)
        df2 <- as.data.frame(hours_set$hour)
        df_aggr_Result <- aggregate(df2, by=list(df2$`hours_set$hour`), FUN = length)
        #Renaming
        names(df_aggr_Result)[names(df_aggr_Result) == "Group.1"] <- "hour"
        names(df_aggr_Result)[names(df_aggr_Result) == "hours_set$hour"] <- "amount of samples"
        
        all_h <- tibble(hour = 0:23)
        
        df_plot = merge(x=all_h,y=df_aggr_Result,by="hour",all=TRUE)
        df_plot[is.na(df_plot)] <- 0
        
        ggplot(df_plot, aes(x = df_plot$hour, y = df_plot$`amount of samples`)) +
          geom_bar(fill = "#0073C2FF", stat = "identity") +
          theme(axis.text.x = element_text(face = "bold", color = "#993333", size = 15),
                axis.text.y = element_text(face = "bold", color = "#993333", size = 15),
                axis.line = element_line(color = "#993333", size = 1)) +
          scale_x_continuous(breaks=seq(0,23,1)) +
          xlab("Hours in a day") + ylab("Amount of samples")
      }
    })  

I already tried with updatPickerInput(), but it don't work. Are there some different approaches?

Thanks in advance!

This can be accomplished using the shinyjs package. Add shinyjs::useShinyjs() to your UI, and then add the observeEvent() below to your server, which utilizes shinyjs::disable()/enable().

Update to tabPanel

tabPanel("Distribution based on SYSTEM", icon = icon("fa-duotone fa-microscope", verify_fa = FALSE),
           sidebarLayout(
             sidebarPanel(
               shinyjs::useShinyjs(), # newly added
               checkboxInput("all_cobas", label = "Select all and only Cobas", value = FALSE),
               selectInput("system", "System:", choices = NULL)),
             mainPanel(
               plotOutput("barplot3"))))

Add to server.

observeEvent(input$all_cobas, {
    if(input$all_cobas == T) {
      shinyjs::disable('system') 
    } else {
      shinyjs::enable('system')
    }
  }, ignoreNULL = T)

Enabled
image

Disabled
image

1 Like

Hi

It seems like logic. I desiced to use a checkbox group instead of a dropdown menu. Now I have another small issue:

From the moment this is solved, I will try this and come back to you!
Thanks already!

Hi

I am trying a different approche. Instead of a selectInput, I use a checkboxgroup. Is it possible that the checkbox (select all and only Cobas) will check all the Cobasses in the checkboxgroup and that you can de-check them?

E.g: check the all cobas box results in all checkboxes with Cobas init will be checked, the Cobas 6800 can be checked of apart (not always needed for analysis).

Thank you!

Since this is different than the original question, it should be created as a new thread.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.