Mark
Mark

Reputation: 2899

Updating multiple radiobuttons in one observer only works on the first radiobutton

I'm completely puzzled why two working lines of code (individually), which reset a respective radiobutton to empty (selected = character(0)) only reset the first line when put together inside 1 'observer'

this is the minimal example:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  tags$script("
    Shiny.addCustomMessageHandler('resetValue', function(variableName) {
                  Shiny.onInputChange(variableName, null);
                  });
                  "),
  radioButtons("radio","Click Me",choices  = c("Choice 1","Choice s"),selected = character(0)),
  radioButtons("radio2","Click Me",choices  = c("Choice 1","Choice s"),selected = character(0)),
  actionButton("clickMe","Click Me to Reset"),
  actionButton("showValue", "Show value")
)

server <- function(input, output, session) {
  observeEvent(input$clickMe,{
    session$sendCustomMessage(type = "resetValue", message ='radio')
    updateRadioButtons(session,"radio",choices  = c("Choice 1","Choice s"),selected = character(0))
    session$sendCustomMessage(type = "resetValue", message ='radio2')

    updateRadioButtons(session,"radio2",choices  = c("Choice 1","Choice s"),selected = character(0))

  })

  observeEvent(input$showValue, {
    print(input$radio)
    print(input$radio2)
  })
}

shinyApp(ui, server)

where the sendcustomMessages are used to reset the values, which is not achieved by selecting character(0), which comes from i.e. here

Upvotes: 2

Views: 457

Answers (1)

TimTeaFan
TimTeaFan

Reputation: 18561

Update

I am not able to figure out why it is not possible to update two radioButtons in one reactiveEvent, but the error seems to stem from setting the selected = argument to character(0). And while the first updateRadioButtons works with character(0) the second doesn't.

Changing character(0)to 0 in the selected = argument of the updateRadioButtons produces the desired result and somehow does not cause said problem/bug. Comparing the buttons values, there seems to be no difference between setting the buttons initially to character(0) or 0. Both ways the button values are NULL. After reseting the values with the custom JavaScript function the value is updated in JS to null.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  tags$script("
              Shiny.addCustomMessageHandler('resetValue', function(variableName) {
              Shiny.onInputChange(variableName, null);
              });
              "),
  radioButtons("radio","Click Me",choices  = c("Choice 1","Choice s"),selected = character(0)),
  radioButtons("radio2","Click Me",choices  = c("Choice 1","Choice s"),selected = character(0)),
  actionButton("clickMe","Click Me to Reset"),
  actionButton("showValue", "Show value")
  )

server <- function(input, output, session) {
  observeEvent(input$clickMe,{
    session$sendCustomMessage(type = "resetValue", message ='radio')
    updateRadioButtons(session,"radio",choices  = c("Choice 1","Choice s"),selected = 0)
    session$sendCustomMessage(type = "resetValue", message ='radio2')

    updateRadioButtons(session,"radio2",choices  = c("Choice 1","Choice s"),selected = 0)

  })

  observeEvent(input$showValue, {
    print(input$radio)
    print(input$radio2)
  })
}

shinyApp(ui, server)

Upvotes: 2

Related Questions