Very slow ggplot

2,080 views
Skip to first unread message

david shanabrook

unread,
Mar 10, 2015, 11:16:23 PM3/10/15
to shinyap...@googlegroups.com
My shinyapps.io app is almost always very very slow.  I put in some show progress code to try to determine what is going on.  Turns out, I generate my ggplot in renderPlot.  there is a last call to incProgress, so I know everything has finished.  It must be somewhere in the mainPanel when it takes seconds to complete.  I can post more code if needed

output$openIntPlot <- renderPlot({

         withProgress(message = 'waiting for plot', value = 0, {

    if (!is.null(chain()))

    getPlot(graphType(),chain(), stockText(), strikes(), lastQuote(), smoothOn(), pinByStrikes())

    else

plotError("No Data.  Either wrong date for this stock, or no prior data")

          incProgress(0.9, detail = "outputing plot")

    })

})

  mainPanel(

    h3(textOutput("caption")), 

    h5(textOutput("subCaption")),

    plotOutput("openIntPlot"),

    googleAnalytics()

  )))



https://bravo.shinyapps.io/openinterest/

Tareef Kawaf

unread,
Mar 11, 2015, 7:35:03 AM3/11/15
to david shanabrook, shinyap...@googlegroups.com
Hi David,
Is your application any faster on a local machine?  ie. is this something that is slow in general, or on shinyapps.io in particular?

-Tareef

--
You received this message because you are subscribed to the Google Groups "ShinyApps Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shinyapps-use...@googlegroups.com.
To post to this group, send email to shinyap...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shinyapps-users/cdf985e1-a7f4-4d70-b0a6-a10f6681dc4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

david shanabrook

unread,
Mar 11, 2015, 9:04:30 AM3/11/15
to shinyap...@googlegroups.com
It is slow on both local and shinyapps.io, although a little worse on the latter.  Confusing, all the code has run, just waiting for shiny to generate the ggplot.  

Tareef Kawaf

unread,
Mar 11, 2015, 9:15:11 AM3/11/15
to david shanabrook, shinyap...@googlegroups.com
Ah... it sounds like a shiny issue.  If you can create a reproducible example, I would post it to the shiny-discuss list along with the version of shiny you are using.  I know there was one iteration of 0.11 that had a significant performance hit that was resolved.
You could try and see if you continue to see the issue if you upgrade to the latest version of shiny from github.

devtools::install_github("rstudio/shiny")



david shanabrook

unread,
Mar 11, 2015, 1:27:26 PM3/11/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
Using 0.11.1  error with the install 
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
  namespace ‘httpuv’ 1.3.0 is already loaded, but >= 1.3.2 is required

Winston Chang

unread,
Mar 11, 2015, 2:52:43 PM3/11/15
to david shanabrook, shinyap...@googlegroups.com
On Wed, Mar 11, 2015 at 12:27 PM, david shanabrook <dhsh...@gmail.com> wrote:
Using 0.11.1  error with the install 
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
  namespace ‘httpuv’ 1.3.0 is already loaded, but >= 1.3.2 is required


Hi David -

You can just reinstall Shiny from CRAN with install.packages('shiny'). You'll also need to restart into a clean R session before installing.

That said, I don't think the performance regression that was in 0.11 was the culprit - it only occurred when there were many tabpanels and conditionalpanels.

david shanabrook

unread,
Mar 11, 2015, 2:59:38 PM3/11/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
Thanks, I did already have 0.11.1  But I did figure out the problem.  I read that one no longer has to  print(thePlot), so I removed the print.  Which worked, but very slowly.

Paul de Barros

unread,
Mar 12, 2015, 8:14:17 AM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
It seems to be running quite fast now.  I just used it, and didn't notice any delay.

david shanabrook

unread,
Mar 12, 2015, 8:40:59 AM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
It is working now because I added the print( back to the renderPlot.  This shouldn't be necessary any more, but in this case it was.  Leaving it out cause the problem. Anyone know why?

Paul de Barros

unread,
Mar 12, 2015, 10:45:08 AM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
Where did you have to include print() in order for it to work?  Did you use that instead of plotOutput() in your ui.R?

david shanabrook

unread,
Mar 12, 2015, 11:00:11 AM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
In the server code
Look at these examples:

output$thePlot  <- renderPlot({
   plot(x,y)
})

or

output$thePlot <- renderPlot({
   print(plot(x,y))
})

then in the ui

mainPanel(plotOutput("thePlot"))


so the 2nd method with the print works.  the first method without the print kind of works, but hangs or with a very slow time to do the plot.  
I had switched from using the print to not, and that caused the problem.  I switched it back and all is fine.

Winston Chang

unread,
Mar 12, 2015, 11:58:44 AM3/12/15
to david shanabrook, shinyap...@googlegroups.com
That's really strange. If you're using the plot() function in renderPlot(), you shouldn't need to call print() on it -- and calling print() shouldn't have any effect on the speed or the output. Can you supply a small reproducible example, or the source code for your app? That will help us get to the bottom of this.

--
You received this message because you are subscribed to the Google Groups "ShinyApps Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shinyapps-use...@googlegroups.com.
To post to this group, send email to shinyap...@googlegroups.com.

Paul de Barros

unread,
Mar 12, 2015, 12:37:19 PM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
I think that the issue might be related to the fact that print(plot(x,y)) print NULL to the console, whereas plot(x,y) prints nothing to the console.  That is only difference I see.  However, what print() and plot() do may depend on what they are given as input.

In your code, is getPlot doing?  I thought that function was for extracting a plot from a plot matrix, but you have way too many parameters for that.

I made a small example based on what has been posted here.  In this example, there is no timing difference in the display of the two graphs.

david shanabrook

unread,
Mar 12, 2015, 1:31:30 PM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
So things get complicated when I post code, but I will give it a try

So here is the server code:

shinyServer(function(input, output, session) {

pinByStrikes <- reactive({input$pinByStrikes})

graphType <- reactive({input$graphType})

stockText <- reactive({toupper(input$stock)})

expText <- reactive({input$yymmdd})

strikes <- reactive({input$strikes})

smoothOn <- reactive({input$smoothOn})

 withProgress(message = 'waiting for data', value = 0, {

lastQuote <- reactive({getQuote(stockText())$Last})

incProgress(0.2, detail = "got quote")

chain <- reactive({getYahooDataReformatted(stockText(), expText())})

incProgress(0.4, detail = "got options")

output$caption <- renderText({paste(stockText(), " $", lastQuote(), " Expiration ", expText())})

output$subCaption <- reactive({wasUpdatedToday(stockText(),chain(),expText())})

if (!is.null(chain)) reactive({cat("good option chain ", stockText(), expText(), "\n")})

incProgress(0.8, detail = "ready to plot")

})

output$openIntPlot <- renderPlot({

withProgress(message = 'waiting for plot', value = 0, {

    if (!is.null(chain()))

    p <- getPlot(graphType(),chain(), stockText(), strikes(), lastQuote(), smoothOn(), pinByStrikes())

else

plotError("No Data.  Either wrong date for this stock, or no prior data")

if (doProgress) incProgress(0.9, detail = "outputting plot")

print(p)

})

})

})


Again when I remove the print(p) and just do the getPlot without the assignment is when I feel I get the problem.  The hang occurs after the last incProgress(0.9detail = "outputting plot"), so I was thinking it didn't matter what getPlot did, as that code ran fine.  getPlot basically just figures out which plot to generate, sets up some parameters, and returns the ggplot.  For the default the ggplot will be this:

p <- ggplot(openInt, aes(x=strike))

    + geom_area(aes(y = putOIfill = "1 put", colour = "1 put",   stat = "bin"), alpha = 0.5)

+ geom_area(aes(y = callOI, fill = "2 call",colour = "2 call"stat = "bin"), alpha = 0.5)

+ geom_point(aes(y=callOI), size=1.5, alpha=.5, color="blue")

+ geom_point(aes(y=putOI),  size=1.5, alpha=.5, color="red")

+ scale_x_continuous(breaks = pretty(sp$lower:sp$upper, n = theN))

    + theme(legend.title = element_blank())

    + theme(axis.text.x=element_text(angle=90,size=theSize))

    + theme(panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank())


I can post all the code if you want.  Or I can take out the print and redeploy the code.  If there was some way of debugging what was happening during the hang this might be useful.

Thanks

David

Winston Chang

unread,
Mar 12, 2015, 1:42:12 PM3/12/15
to david shanabrook, shinyap...@googlegroups.com
You can debug it by putting browser() in your code and then stepping through it as it runs. See the "Browsing arbitrary code" section of Hadley's Advanced R book:
  http://adv-r.had.co.nz/Exceptions-Debugging.html

If you could supply a runnable app, that would help.



Paul de Barros

unread,
Mar 12, 2015, 1:56:22 PM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
What packages do you have loaded?  There are a bunch of functions in there that I don't recognize.

david shanabrook

unread,
Mar 12, 2015, 2:45:17 PM3/12/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
I didn't post all of my code, probably my functions.  I will do this later tonight when I get time.  But I am still wondering why any of it would matter since as far as I can tell, none of my code is running during the hang.  And all of this code just generates a ggplot like I posted.

library(shiny)

library(quantmod)

library(ggplot2)

Message has been deleted

david shanabrook

unread,
Mar 22, 2015, 11:39:53 PM3/22/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
I cleaned up the code a lot, and got it so it works fast locally.  but it still sometimes takes a long time to plot on shinyio server.  All of my code has completed, just waiting for the plot to render.  The code is now on github.  I don't know how to debug it any further since it now seems constantly fast when run local.  

billy am

unread,
Mar 23, 2015, 1:16:24 AM3/23/15
to david shanabrook, shinyap...@googlegroups.com
Hi David ,

Thanks for sharing the code. Managed to install it on my server but
doesn't seem like ggplot is loading?

http://dev.use-r.com/openinterest/

Regards
Billy


----------------------------------------------------------------------------------
|

http://billyam.com || http://use-r.com || http://shinyserver.com (BETA)

SAS Certified Base Programmer for SAS 9
Oracle SQL Expert(11g)
> https://groups.google.com/d/msgid/shinyapps-users/9b646200-dc06-4316-aa9f-52af018f364b%40googlegroups.com.

david shanabrook

unread,
Mar 23, 2015, 9:07:42 AM3/23/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
The version on github should be the same as what is running on shinyio, and it seems to be working there.  Again the problem I am having is now only on shinyio server, locally it is running fine.

david shanabrook

unread,
Mar 23, 2015, 9:11:33 AM3/23/15
to shinyap...@googlegroups.com, dhsh...@gmail.com
So locally it now is running fine.  On shinyio it is at times very slow to generate plots.  It seems to be a problem only when plots contain more data.  So the default now is fast, but when changing to the month expiration, Apr* 17, it again runs slowly.  Same when strikes are increased to all.  Could this make a difference in the rendering and that is where the problem is with shinyio?
Reply all
Reply to author
Forward
0 new messages