首页 > 解决方案 > R Shiny 直到几页刷新后才显示情节

问题描述

我是 Shiny 的新手,一直在开发一个显示绘图图表的网络应用程序;我仍在本地机器上开发应用程序。当我在打开 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)

我不知道我是否遗漏了一些简单的东西或什么。感谢所有帮助。

标签: rshiny

解决方案


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

您需要添加一个eventReactive方法

查看r 闪亮的操作按钮和数据表输出

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)

推荐阅读