【发布时间】:2018-04-26 12:02:42
【问题描述】:
我有一个运行模拟的闪亮应用。目标是向用户展示中间的计算步骤。
如何强制闪亮更新情节?
MWE 应该是这样的
library(shiny)
server <- function(input, output, session) {
# base plot as a placeholder
output$myplot <- renderPlot(plot(1:1, main = "Placeholder"))
# wait until the button is triggered
observeEvent(input$run, {
print("Do some calculations in 3 steps")
for (i in seq_len(3)) {
print("Do some calculations")
# ...
x <- seq_len(i * 100)
y <- (x + 1)^2 - 1 # this will do for now
print("Plot the data ")
# ISSUE HERE!
# this should render the current step of the simulation, instead it
# renders only after the whole code is run (i.e., after step 3)
output$myplot <- renderPlot(plot(x, y, main = sprintf("Round %i", i), type = "l"))
print("Wait for 1 second for the user to appreciate the plot...")
Sys.sleep(1)
}
})
}
ui <- fluidPage(
actionButton("run", "START"),
plotOutput("myplot")
)
shinyApp(ui = ui, server = server)
问题是,闪亮运行代码并在模拟结束时生成一个图,但是,我想在每个模拟步骤获得一个图(显示至少一秒钟)。
非常感谢任何帮助/提示。
附录
我查看了这个post,但是用绘图/renderPlot 替换文本不会产生正确的结果。
【问题讨论】:
-
您需要循环 UI 和服务器输出以适应每个绘图。您不能只有一个绘图输出变量并期望循环产生许多绘图。
-
@RyanMorton 你手头有例子吗?
-
尝试添加这样的滑块输入 `sliderInput("format", "Custom Format:", min = 0, max = 3, value = 0, step = 1, animate = TRUE), `