Stored Input values in shiny widgets?

I am trying to understand the value stored in shiny inputwidgets. I have written below code which outputs a sliderInput and based on the value of sliderInput, another o/p element is generated i.e.

If sliderInput value > 30 then it generates groupedradiobutton else it generates a file upload button

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 ){
      # render grouped radio buttons  
      radioGroupButtons(inputId = "opbAppendRemoveMonthlyOption",choices =c("Append","Continue"), status = "success",
                        direction = "horizontal",justified = F,checkIcon = list(yes=icon("ok",lib="glyphicon")),selected = character(0))

    }else{
      # render upload button
      fileInput(inputId="btnUploadMonthlyFile",label = "Upload Monthly Data file") 
    }
  })

  output$RadioButtonValue<-renderText({
    x<-(input$opbAppendRemoveMonthlyOption)
    return(x)

  })
}

shinyApp(ui = ui, server = server)

Situation:-

Step 1 - I changed the value of sliderInput to 31 which generates groupedradiobutton and then I select Append from the radiobutton

Step 2 - I again changed the value of sliderInput to 32 which regenerates the groupedradiobutton, which I believe should reset the value of input$opbAppendRemoveMonthlyOption used in output$RadioButtonValue block as Null, but instead of that it still retains the value selected in Step1

I think this is due to the fact that when I refer input$opbAppendRemoveMonthlyOption in output$RadioButtonValue it returns the cached value? if that is so how can set the value to default every time I change the value of sliderInput?

Hi, you're right, it's returning the cached value. This example illustrates that the namespace of values produced by inputs at some point is separate from whether or not the input is still visible.

I think the way to achieve what you expect is to introduce an intermediate reactive value to represent whether or not the input is visible. This way you can show the value produced by the input conditionally.

Here's an example:

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) {
  showOptions <- reactive(input$num > 30)
  
  output$uploadorSelect <- renderUI({
    if(showOptions()) {
      # render grouped radio buttons  
      radioGroupButtons(inputId = "opbAppendRemoveMonthlyOption",choices =c("Append","Continue"), status = "success",
                        direction = "horizontal",justified = F,checkIcon = list(yes=icon("ok",lib="glyphicon")),selected = character(0))
    } else {
      # render upload button
      fileInput(inputId="btnUploadMonthlyFile",label = "Upload Monthly Data file") 
    }
  })

  output$RadioButtonValue <- renderText({
    if (showOptions()) input$opbAppendRemoveMonthlyOption else NA
  })
}

shinyApp(ui = ui, server = server)
1 Like