1
votes

One of my apps is displaying a ggplot via uiOutput('plot.ui') and plot.ui is rendered through renderUI().

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

The code works, but it is very laggy. It seems that this is a two-step process. In my app, it first renders 'plot' (which is a ggplot rendered by renderPlot), then it resizes the plot according the specified width and height. The lag between the two steps is significant (about 3 seconds). I checked it by wrapping a withProgress() around plotOutput(), and the problem still exists. I am wondering why this problem exists and if there is any way to solve it.

A small example is attached to illustrate this problem.

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))
    })
  }
)

Thank you very much for your help!

1
It's not shiny... just running plot(runif(100000,1,100),runif(100000,1,100)) takes a second or two on my machine. That's a lot of points to plot. - cory

1 Answers

0
votes

I had the similar issue, so if you had fixed the problem in another way, let me know. What I attempted to to was adjust the size of plotOutput depending on what I was plotting (with certain inputs, I had horizontal bar plot with 1 bar or 10 bars.. needed to adjust the height accordingly.

Solution 1) Adjust the height of renderplot() As explained by jcheng5 here. See if this solves the issue

Solution 2) Define a plot function, use 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())
}

This works for me. See if this solves the issue.