【问题标题】:Shiny: Why updateSliderInput works only one time?Shiny:为什么 updateSliderInput 只能工作一次?
【发布时间】:2021-03-30 09:53:47
【问题描述】:

我正在尝试编写一个闪亮的应用程序,一旦我在按下按钮时执行任意代码,它就会动态地update slider input 最大值:

library(shiny)

ui <- fluidPage(

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            actionButton(inputId ="refresh", label = 'boom')
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
    
    counter <- reactiveValues(c = nrow(faithful))

    observeEvent(input$refresh, {
        
        faithful <- faithful[-1:-10, ]
        counter$c <- nrow(faithful)
        updateSliderInput(session, "bins", min = 1, max = counter$c, value = counter/2)
    })
    
    
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

不幸的是,滑块输入更新只能工作一次!文档示例建议使用 observe() 构造观察一些反应值,但是我希望在使用 observeEvent() 时,这样的功能也应该起作用。这里可能有什么问题?

感谢您的帮助!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这里的问题不是updateSliderInput,而是counter 不是反应式的,所以它在应用程序内部不是持久的。只需将其转换为本示例中的反应式,它就可以正常工作。

    library(shiny)
    
    ui <- fluidPage(
      
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30),
          actionButton(inputId ="refresh", label = 'boom')
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("distPlot")
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output, session) {
      
      counter <- reactiveValues(c = 10)
      
      observeEvent(input$refresh, {
        counter$c <- counter$c + 10
        updateSliderInput(session, "bins", min = 1, max = counter$c, value = counter$c/2)
      })
      
      
      output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 非常感谢您的回答!我现在可以看到如何使用reactiveValues() 构造。我仍在为reactive 的概念而苦苦挣扎:我怎样才能执行更多代码?如果我的值c 是一些任意代码的结果怎么办?
    • 刚刚添加了一个代码示例,但我无法理解如何访问闪亮的内存区域。一旦数据对象在“全局”环境中,任意代码就可以在 observeEvent 构造中运行
    • 要了解如何使用响应式,我建议您查看一些有关响应式的 Shiny 教程。在这种情况下定义一个全局变量是可行的,但在这里不是很好的做法。在这种情况下应该使用 Reactive。看看reactivereactiveValreactiveValueseventReactive函数。
    猜你喜欢
    • 2019-10-08
    • 2018-05-20
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2012-03-09
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多