【发布时间】: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() 时,这样的功能也应该起作用。这里可能有什么问题?
感谢您的帮助!
【问题讨论】: