Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 809
Rohit Saluja Avatar asked Jan 08 '18 14:01

Rohit Saluja


1 Answers

Your app is set up correctly, but the issue appears to be with the message that the new value for opbAppendRemoveMonthlyOption is "nothing selected" gets lost. For example, if you change the default selected option to "Continue", then it does get reset correctly each time the slider is changed.

Normally, you could also use the corresponding update function in a reactive expression to change the value of an input like this:

  observeEvent({input$num}, {
    updateRadioGroupButtons(session, "opbAppendRemoveMonthlyOption",
                            selected = character(0))
  })

However, just as in the creation of the input, this message is ignored when attempting to set the value back to the unselected state but works if setting it to one of the options. Note that the use of the unselected state is discouraged in the Shiny documentation (?radioButtons), so it may not be fully supported.

If you need to represent a "None selected" state, it's possible to default the radio buttons to have no options selected by using selected = character(0). However, this is not recommended, as it gives the user no way to return to that state once they've made a selection. Instead, consider having the first of your choices be c("None selected" = "").

You can work around this with a little bit of JavaScript to force the input value to reset whenever the slider is clicked.

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  tags$div(
    sliderInput(inputId = "num", 
                label = "Choose a number", 
                value = 25, min = 1, max = 100),
    onchange = "Shiny.onInputChange('opbAppendRemoveMonthlyOption', '')"
  ),
  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)

More info Shiny<->JavaScript messaging from RStudio.com

like image 142
W. Murphy Avatar answered Sep 28 '22 05:09

W. Murphy