Switch

#| standalone: true
#| components: [viewer]
#| viewerHeight: 200

library(shiny)
library(bslib)

ui <- page_fluid(
  input_switch("switch", "Switch"), 
  verbatimTextOutput("value")
)

server <- function(input, output) {
  output$value <- renderText({input$switch})
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)

ui <- page_fluid(
  input_switch("switch", "Switch"), 
  verbatimTextOutput("value")
)

server <- function(input, output) {
  output$value <- renderText({input$switch})
}

shinyApp(ui = ui, server = server)
No matching items

Relevant Functions

  • input_switch
    input_switch(id, label, value = FALSE, width = NULL)

No matching items

Details

A switch allows you to select between logical values.

To add a switch to your app:

  1. Add bslib::input_switch() to the UI of your app to create a switch. Where you call this function will determine where the switch will appear within the app’s layout.

  2. Specify the id and label parameters of input_switch() to define the identifier and label of the switch.

  3. By default, the value parameter, which defines the switch’s initial value, is FALSE. If you’d like the initial value to be TRUE, set value equal to TRUE.

The value of an input component is accessible as a reactive value within the server() function. To access the value of a switch:

  1. Use input$<switch_id> (e.g., input$switch) to access the value of the switch. The server value of a switch is TRUE if checked and FALSE otherwise.