Skip to content

[Bug]: update_checkbox_group requires different selected than input_checkbox_group #2272

@QuintonBaker-USDA

Description

@QuintonBaker-USDA

Component

UI (ui.*)

Severity

P3 - Low (minor inconvenience)

Shiny Version

1.61

Python Version

3.14.2

Minimal Reproducible Example

from shiny import App, reactive, ui

DICT = {"Foo": {0: "a", 1: "b", 2: "c"}, "Bar": {0: "alpha", 1: "beta", 2: "gamma"}}

problem_h2 = """
Bug: update_checkbox_group() requires different 'selected' setup for choices 
with integer keys than input_checkbox_group()
"""

problem_str = """
input_checkbox_group accepts selected keys as ints, but update_checkbox_group does not.
The intention is to have all the options checked by default. There is no problem when 
initiating the checkbox group, but there is a problem when it updates.
\n Bonus idea:\n
It would be great if you could just pass a bool as an argument to check all boxes passed.
"""

checkbox_init = {
    "label": "Select multiple options:",
    "choices": DICT["Foo"],
    "selected": [k for k in DICT["Foo"].keys()],
}

app_ui = ui.page_fluid(
    ui.h2(problem_h2),
    ui.p(problem_str),
    ui.input_radio_buttons(
        "select_key", "Select a dictionary:", list(DICT.keys()), selected="Foo"
    ),
    ui.card(
        ui.card_header("Bugged checkbox_group"),
        ui.p(
            "After selecting a dict above, the options will be unchecked. They should be checked."
        ),
        ui.input_checkbox_group("check_group_bad", **checkbox_init),
    ),
    ui.card(
        ui.card_header("This checkbox_group works properly"),
        ui.p("But it required an extra step of converting keys to strings"),
        ui.input_checkbox_group("check_group_good", **checkbox_init),
    ),
)


def server(input, output, session):
    @reactive.effect
    @reactive.event(input.select_key, ignore_init=True)
    def _():
        choices = DICT[input.select_key()]
        ui.update_checkbox_group(
            "check_group_bad", choices=choices, selected=[k for k in choices.keys()]
        )
        ui.update_checkbox_group(
            "check_group_good",
            choices=choices,
            selected=[str(k) for k in choices.keys()],
        )


app = App(app_ui, server)

Behavior

Current: update_checkbox_group() with selected=some_dict where the type of some_dict is dict[int, str], does not check the boxes. This is unexpected, because input_checkbox_group() accepts dictionaries with int keys and quietly converts them to strings.
Expected: update_checkbox_group() should quietly convert dictionary integers to string the same way input_checkbox_group() does for the selected parameter.
Current workaound: converting int keys from dict to string works just fine. e.g selected=[str(k) for k in choices_dict.keys()]

Error Messages (if any)

No errors, shiny silently doesn't check any boxes when none of the `selected=` hit.

Environment

OS: Windows 11
Browser: Chrome
Dependencies: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions