简体   繁体   English

闪亮的应用程序不更新隐藏的滑块

[英]Shiny App not updating hidden sliders

I currently have a basic R Shiny app that consists of a couple of sliders whose values are outputted in a table. 我目前有一个基本的R Shiny应用程序,它包含几个滑块,其值在表格中输出。 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. 问题是当我使用shinyjs::hidden滑块(以改进UI)时,输出表不会更新。 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? 有没有办法让Shiny更新滑块和输出,即使它们被隐藏了?

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. 您可以查看outputOptions函数suspendWhenHidden参数。

You could use some css to position the slider outside the window. 您可以使用一些CSS将滑块定位在窗口外。 You will have to wrap the sliderInput in a div tag and asign this style to it: 您必须将sliderInput包装在div标记中并将此样式设置为它:

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

Demo Shiny with css: 用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. 但它也应该与shinjys::hidden一起工作,正如你在下一个演示中看到的那样。

Shiny Demo with shinjys: 与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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM