Radio Buttons

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

library(shiny)
library(bslib)

ui <- page_fluid(
  radioButtons( 
    inputId = "radio", 
    label = "Radio buttons", 
    choices = list( 
      "Option 1" = 1, 
      "Option 2" = 2, 
      "Option 3" = 3 
    ) 
  ), 
  textOutput("choice")
)

server <- function(input, output) {
  output$choice <- renderText({input$radio})
}

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

ui <- page_fluid(
  radioButtons( 
    inputId = "radio", 
    label = "Radio buttons", 
    choices = list( 
      "Option 1" = 1, 
      "Option 2" = 2, 
      "Option 3" = 3 
    ) 
  ), 
  textOutput("choice")
)

server <- function(input, output) {
  output$choice <- renderText({input$radio})
}

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

Relevant Functions

  • radioButtons
    radioButtons(inputId, label, choices = NULL, selected = NULL, inline = FALSE, width = NULL, choiceNames = NULL, choiceValues = NULL)

No matching items

Details

Use a set of radio buttons to select an item from a list.

To add radio buttons to your app:

  1. Add radioButtons() to the UI of your app to create a set of radio buttons. Where you call this function will determine where the radio buttons will appear within the app’s layout.

  2. Specify the inputId and label parameters of radioButtons() to define the identifier and label of the set of radio buttons.

  3. Specify the value and label that accompanies each radio button using the choices parameter. Set choices to a list of values. If the list contains names, radioButtons() will use the names as button labels. Otherwise, radioButtons() will use the values as button labels. When a user clicks a button, radioButtons() will return the value associated with it as a reactive variable.

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

  1. Use input$<radio_buttons_inputId> to access the value of a radio button set (e.g., input$radio). The server value of a set of radio buttons is a string containing the selected value.