【问题标题】:laggy Shiny uiOutput滞后闪亮的uiOutput
【发布时间】:2015-08-03 15:56:21
【问题描述】:

我的一个应用程序通过uiOutput('plot.ui') 显示一个ggplot,而plot.ui 通过renderUI() 呈现。

  output$plot.ui=renderUI({
   plotOutput('plot', width=a function(), height=a function())
   }) 

代码有效,但非常滞后。这似乎是一个两步的过程。在我的应用程序中,它首先渲染“绘图”(这是由 renderPlot 渲染的 ggplot),然后根据指定的宽度和高度调整绘图的大小。两个步骤之间的延迟很明显(大约 3 秒)。我通过在 plotOutput() 周围包裹一个 withProgress() 来检查它,问题仍然存在。我想知道为什么会存在这个问题,以及是否有任何方法可以解决它。

附上一个小例子来说明这个问题。

library(shiny)
shinyApp(
  ui=shinyUI(
    pageWithSidebar(
      titlePanel('test'),
      sidebarPanel(
        sliderInput('width','Width: ', min=0,max=1000,value=100),
        sliderInput('height','Height: ',  min=0,max=1000,value=100)

        ),
    mainPanel(uiOutput('plot.ui'))
      )
    ),
  server=function(input,output){

    output$plot.ui=renderUI({
      plotOutput('plot',width=input$width,height=input$height)

    })
    output$plot=renderPlot({
      plot(runif(100000,1,100),runif(100000,1,100))
    })
  }
)

非常感谢您的帮助!

【问题讨论】:

  • 它不是闪亮的......只是在我的机器上运行plot(runif(100000,1,100),runif(100000,1,100)) 需要一两秒钟。有很多要点要绘制。

标签: r shiny


【解决方案1】:

我遇到了类似的问题,所以如果您以其他方式解决了问题,请告诉我。我试图根据我正在绘制的内容调整 plotOutput 的大小(对于某些输入,我有 1 条或 10 条的水平条形图。需要相应地调整高度。

解决方案1)调整renderplot()的高度 正如jcheng5 here 所解释的那样。看看能不能解决问题

解决方案2)定义一个绘图函数,使用isolate()

# Define a function that returns a plot
plot_function <- function(){
  plot(runif(100000,1,100),runif(100000,1,100)
}

# reactive UI and adjust the height here
output$plot.ui=renderUI({
   plotOutput("plot", height = -------------)
})
# call plot_function but use isolate()
output$plot <- renderPlot({
  isolate(plot_function())
}

这对我有用。看看这是否能解决问题。

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 2020-10-17
    • 2016-08-05
    • 2020-08-22
    • 2015-11-13
    • 2019-09-07
    • 2015-05-05
    • 2021-01-27
    • 2015-04-01
    相关资源
    最近更新 更多