Radio Buttons
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
## file: app.py
from shiny import App, render, ui
app_ui = ui.page_fluid(
ui.input_radio_buttons(
"radio",
"Radio buttons",
{"1": "Option 1", "2": "Option 2", "3": "Option 3"},
),
ui.output_ui("value"),
{"class": "vh-100 d-flex align-items-center px-4"},
)
def server(input, output, session):
@render.ui
def value():
return input.radio()
app = App(app_ui, server)
Relevant Functions
-
ui.input_radio_buttons
ui.input_radio_buttons(id, label, choices, *, selected=None, inline=False, width=None)
Details
Use a set of radio buttons to select an item from a list.
To add radio buttons to your app:
Add
ui.input_radio_buttons()
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.Specify the
id
andlabel
parameters ofui.input_radio_buttons()
to define the identifier and label of the set of radio buttons.Specify the value and label that accompanies each radio button using the
choices
parameter.choices
can be either a list or a dictionary. Ifchoices
is a list, the list elements become the radio button values and labels. Ifchoices
is a dictionary,ui.input_radio_buttons()
uses the dictionary keys as the button values and the dictionary values as the button labels.
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:
- Use
input.<radio_buttons_id>()
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.