在下面的MRE中,要求用户填写一张表格,从中绘制曲线。为了模拟在生成图形输出之前在表上进行的一些计算,我添加了一个
Sys.sleep()
.您将看到,如果表格填写速度足够快,即比
系统。睡眠()
,应用程序将无法使用,必须终止。
我认为这是因为表格渲染是在计算/睡眠和绘图渲染之后进行的。我应该如何解决这个问题,使应用程序能够实时响应并仍然可用?
library(shiny)
library(rhandsontable)
library(ggplot2)
DF <- data.frame(x=integer(0), y=integer(0))
ui <- shinyUI(fluidPage(
mainPanel(
rHandsontableOutput("hot"),
plotOutput("plot1")
)
))
server <- shinyServer(function(input, output) {
values <- reactiveValues()
observe({
if (!is.null(input$hot)) {
DF <- hot_to_r(input$hot)
} else {
if (is.null(values[["DF"]]))
DF <- DF
else
DF <- values[["DF"]]
}
values[["DF"]] <- DF
})
output$hot <- renderRHandsontable({
rhandsontable(values[["DF"]], stretchH = "all", minRows=5)
})
output$plot1 <- renderPlot({
table <- {
Sys.sleep(.4)
values[["DF"]]
}
ggplot(data=table) + geom_line(aes(x=x, y=y))
})
})
shinyApp(ui=ui, server=server)