Salvador
Salvador

Reputation: 1565

How to enable/disable checkboxInput when certain panel is selected

I am trying to disable the checkbox if the 'Uno' tab is selected but enable it if the 'Dos' tab is selected. How can I do that in the server with an event?

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxInput("myCheckbox", "Enable me", value = FALSE)
    ),
    
    mainPanel(
      tabsetPanel(id = "myTabs",
                  tabPanel("Uno", value = "tab1", "This is tab Uno"),
                  tabPanel("Dos", value = "tab2", "This is tab Dos")
      )
    )
  ),
)

server <- function(input, output, session) {
  session$onSessionEnded(function() {
    stopApp()
  })
}
shinyApp(ui, server)

Upvotes: 0

Views: 16

Answers (1)

Michael Dewar
Michael Dewar

Reputation: 3408

You can use the shinyjs package to enable() and disable() an input. Use an observer to watch the value of the chosen tab.

library(shiny)
library(shinyjs)

ui <- fluidPage(
    useShinyjs(),
    sidebarLayout(
        sidebarPanel(
            disabled(checkboxInput("myCheckbox", "Enable me", value = FALSE))
        ),
        
        mainPanel(
            tabsetPanel(id = "myTabs",
                        tabPanel("Uno", value = "tab1", "This is tab Uno"),
                        tabPanel("Dos", value = "tab2", "This is tab Dos")
            )
        )
    ),
)

server <- function(input, output, session) {
    session$onSessionEnded(function() {
        stopApp()
    })
    
    observeEvent(input$myTabs,{
        if(input$myTabs == "tab1"){
            disable("myCheckbox")
        } else {
            enable("myCheckbox")
        }
    })
}
shinyApp(ui, server)

Upvotes: 1

Related Questions