You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny动态输入部件值缓存问题:如何重置radioGroupButtons选中值?

Fixing Radio Button Value Persistence in Dynamic Shiny UI

Great question! The issue you're seeing comes down to how Shiny handles input value caching: when you re-render a radioGroupButtons component with the same inputId, Shiny holds onto the previously selected value instead of honoring the selected parameter you set. Here are two straightforward fixes to resolve this:

Method 1: Explicitly Reset Values with updateRadioGroupButtons

We can add an observer that listens for slider changes, then explicitly resets the radio button selection whenever the slider updates (and the radio buttons are active):

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100),
  uiOutput("uploadorSelect"),
  textOutput("RadioButtonValue")
)

server <- function(input, output, session) {
  output$uploadorSelect <- renderUI({
    x <- input$num
    if(x > 30){
      radioGroupButtons(
        inputId = "opbAppendRemoveMonthlyOption",
        choices = c("Append","Continue"), 
        status = "success", 
        direction = "horizontal",
        justified = FALSE,
        checkIcon = list(yes=icon("ok",lib="glyphicon")),
        selected = character(0)
      )
    } else {
      fileInput(inputId="btnUploadMonthlyFile",label = "Upload Monthly Data file")
    }
  })
  
  # Reset radio button whenever slider value changes
  observeEvent(input$num, {
    if(input$num > 30){
      updateRadioGroupButtons(
        session = session,
        inputId = "opbAppendRemoveMonthlyOption",
        selected = character(0)
      )
    }
  })
  
  output$RadioButtonValue <- renderText({
    input$opbAppendRemoveMonthlyOption
  })
}

shinyApp(ui = ui, server = server)

How this works:

  • The observeEvent triggers every time the slider value changes.
  • When the slider is above 30 (so radio buttons are visible), it calls updateRadioGroupButtons to override Shiny's cached value and reset the selection to empty.

Method 2: Use a Dynamic Input ID (Alternative)

Another approach is to make the radio button's inputId dependent on the slider value. This way, each slider change creates a brand-new input component, so Shiny won't use any cached values:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100),
  uiOutput("uploadorSelect"),
  textOutput("RadioButtonValue")
)

server <- function(input, output) {
  output$uploadorSelect <- renderUI({
    x <- input$num
    if(x > 30){
      # Add slider value to inputId to create a unique component each time
      radioId <- paste0("opbAppendRemoveMonthlyOption_", x)
      radioGroupButtons(
        inputId = radioId,
        choices = c("Append","Continue"), 
        status = "success", 
        direction = "horizontal",
        justified = FALSE,
        checkIcon = list(yes=icon("ok",lib="glyphicon")),
        selected = character(0)
      )
    } else {
      fileInput(inputId="btnUploadMonthlyFile",label = "Upload Monthly Data file")
    }
  })
  
  # Dynamically fetch the current radio button value
  output$RadioButtonValue <- renderText({
    x <- input$num
    if(x > 30){
      radioId <- paste0("opbAppendRemoveMonthlyOption_", x)
      input[[radioId]]
    } else {
      NULL
    }
  })
}

shinyApp(ui = ui, server = server)

Note:

This method requires you to dynamically reference the input ID in your output functions, but it's a foolproof way to avoid caching issues entirely.

Either solution will fix your problem: when you adjust the slider from 31 to 32, the radio buttons will now reset to their default empty selection as expected.

内容的提问来源于stack exchange,提问作者Rohit Saluja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:40:57