R Shiny在刷新几页后才显示图

时间:2019-07-12 15:13:31

标签: r shiny

我是Shiny的新手,并且一直在开发一个显示绘图图表的Web应用程序;我仍在本地计算机上开发该应用程序。打开RGui后首次运行该应用程序时,即使单击“绘制”按钮,该Web应用程序仍会运行但不会呈现图表。我必须刷新网页一次或两次,然后图表才会呈现。绘制图表后,该R会话持续时间内问题就消失了。刷新页面或重新启动闪亮的程序将继续呈现图表,直到RGui关闭。下次我打开RGui并运行应用程序时,该问题可靠地重现。

我在有光泽和渲染失败的情况下搜索的所有现有问题和答案都没有回答我的问题。

经过几次尝试寻找程序错误的尝试后,我将其归结为以下代码(对我而言)看起来应该可以正常工作,但仍然存在问题:

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("Plot"),
  actionButton("drawPlotButton", "Draw")
)

server = function(input, output)
{
  output$Plot = renderPlotly({
    input$drawPlotButton
    return(plot_ly(mtcars, x = ~hp, y = ~mpg, type = "scatter", mode = "markers"))
  })
}

shinyApp(ui = ui, server = server)

我无法确定我是否缺少简单的东西或什么。感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

通常对我来说运行良好,如初始化时的情节渲染所示。 但是,如果您的目标是仅在单击 Draw 按钮时才绘制图形,则:

您需要添加一个eventReactive方法

请参见r Shiny action button and data table output

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("Plot"),
  actionButton("drawPlotButton", "Draw")
)

server = function(input, output)
{
  plot_d <- eventReactive(input$drawPlotButton, {
    plot_ly(mtcars, x = ~hp, y = ~mpg, type = "scatter", mode = "markers")
  })

  output$Plot = renderPlotly({
    plot_d()
  })
}

shinyApp(ui = ui, server = server)
相关问题