r - 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)
我不知道我是否遗漏了一些简单的东西或什么。感谢所有帮助。
解决方案
通常对我来说运行良好,因为在初始化时的情节渲染中。但是,如果您的目标是仅在单击“绘图”按钮时绘制图形,则:
您需要添加一个eventReactive
方法
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)
推荐阅读
- swift - open(FileManager.default.fileSystemRepresentation(withPath: path), O_EVTONLY) 返回 -1
- c# - C# COM 服务器从 GetIDsOfNames 返回 E_POINTER
- json - NiFi EvaluateJsonPath 数组长度
- c# - 如何在net core 3中同步任务之间的方法调用
- mongodb - PySpark 无法连接到 MongoDB,但命令行可以
- php - 如何在 Laravel 中输出保存的时区速记(GMT、UTC、EST 等)
- pointers - 为什么必须对指针使用星号而不是结构指针?
- c++ - 为什么我总是得到 Ř 而不是 č?(C++,视觉工作室)
- python - 如何用相同的键连接字典的值
- clojure - 编写一个尽可能延迟的类似展开器的函数来生成任意分解