Shiny App not updating hidden sliders

1.1k views Asked by At

I currently have a basic R Shiny app that consists of a couple of sliders whose values are outputted in a table. The table is rendered using as follows:

output$profile<-renderTable({
    data.table(Name=userNames[input$userC,Name],
        Value=input$FirstSlider,
        # More data here
    )
})

I also have three 'preset' buttons that change the values of the sliders to one of three presets when clicked:

observe({
    if(input$Preset==1){
        updateSliderInput(session,"FirstSlider",value=1)
    } else if(input$Preset==2) {
        updateSliderInput(session,"FirstSlider",value=2)
    } else {
        updateSliderInput(session,"FirstSlider",value=3)
    }
}

The problem is that when I hide the sliders using shinyjs::hidden (to improve the UI), the output table is not updated. Even when I put the sliders on another tab, the output is only updated when switching to that tab.

Is there a way to make Shiny update the sliders and the output, even though they are hidden?

2

There are 2 answers

2
DeanAttali On

This is because of the way shiny was designed. For performance reasons, any input that is not currently visible doesn't get executed. You can look into the outputOptions function suspendWhenHidden parameter.

0
SeGa On

You could use some css to position the slider outside the window. You will have to wrap the sliderInput in a div tag and asign this style to it:

#tohide {
  left: 999999px;
  position: absolute;
}

Demo Shiny with css:

library(shiny)
library(DT)
library(data.table)

css <- "
#tohide {
  left: -999999px;
  position: absolute;
}"

ui <- fluidPage(
  tags$head(tags$style(css)),
  sliderInput("Preset", label = "Preset", 1, 6, 1, 1),
  tags$div(id="tohide",
           sliderInput("FirstSlider", 
                       label = "FirstSlider", 1, 3, 1, 1)
           ),
  tableOutput("profile")
)

server <- function(input, output, session) {
  output$profile<-renderTable({
    data.table(Name="a",
               Value=input$FirstSlider
    )
  })
  observe({
    if(input$Preset==1){
      updateSliderInput(session,"FirstSlider",value=1)
    } else if(input$Preset==2) {
      updateSliderInput(session,"FirstSlider",value=2)
    } else {
      updateSliderInput(session,"FirstSlider",value=3)
    }
  })
}

shinyApp(ui, server)

But it should work with shinjys::hidden too, as you can see in the next demo.

Shiny Demo with shinjys:

library(shiny)
library(DT)
library(data.table)
library(shinyjs)



ui <- fluidPage(
  useShinyjs(),
  tags$head(tags$style(css)),
  sliderInput("Preset", label = "Preset", 1, 6, 1, 1),
  shinyjs::hidden(sliderInput("FirstSlider", 
                     label = "FirstSlider", 1, 3, 1, 1)),
  tableOutput("profile")
)

server <- function(input, output, session) {
  output$profile<-renderTable({
    data.table(Name="a",
               Value=input$FirstSlider
    )
  })
  observe({
    if(input$Preset==1){
      updateSliderInput(session,"FirstSlider",value=1)
    } else if(input$Preset==2) {
      updateSliderInput(session,"FirstSlider",value=2)
    } else {
      updateSliderInput(session,"FirstSlider",value=3)
    }
  })
}

shinyApp(ui, server)