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:
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.Specify the
idandlabelparameters ofinput_switch()to define the identifier and label of the switch.By default, the
valueparameter, which defines the switch’s initial value, isFALSE. If you’d like the initial value to beTRUE, setvalueequal toTRUE.
The value of an input component is accessible as a reactive value within the server() function. To access the value of a switch:
- Use
input$<switch_id>(e.g.,input$switch) to access the value of the switch. The server value of a switch isTRUEif checked andFALSEotherwise.