How can I use compiler package to compile shiny ui / server code to speed up shiny applications?

Hi gentlemen,

How can I use compiler package to compile shiny ui / server code to speed up shiny applications?

My current shiny app has first time load time around 4 seconds. After the first load refresh app only takes around 1 second.

My server is xeon 2680 duo and in the same 1000mb network with test pc. Not network issue.

I suspect to precompile shiny ui/server code using compiler library can have 10x time increase which reduce first-time page load from 4 seconds to 0.4 seconds.

I have proof that when I use compiler package compile my other standard alone R application has 20x time speed up.

The question is how to pre-compile shiny server/ui code?

Many thanks,


I think it is very unlikely that you'll get a 10x speedup in Shiny app load times by precompiling the code. The R byte code compiler in general does not speed things up by that much. I realize that you said you got a 20x speedup in your code, but that only happens in unusual cases. Also, in the vast majority of Shiny applications, most of the processing in startup is not spent in the application code you wrote, but in functions that your application calls, and (with recent versions of R), that code will generally be already compiled.

In recent versions of R, the JIT compiler is enabled by default, and a function is compiled (I believe) the second time it is called. When you call reactive(), observe(), or a render function like renderPlot(), it creates a function. There currently isn't a way to tell Shiny to compile those objects/functions when they're created.

Have you tried using profvis to profile your code? It is very helpful for finding bottlenecks.

thanks much for your reply. The most time consumed on my apps is quite some reactivepoll and database query function. I think I need further dig into the performance tuning of the code if I have too. I was wondering to use some quick fix to improve performance like compiler package, but obviously it's not possible for shiny application as you mentioned. Your comments are appreciated, I need to learn how to use profvis to profile my code.

first time shiny load 4 seconds is too much. the second load 1 second is much better... do you know any automatic warm-up function is available? or I have to manually write one to call my shiny app in advance or keep it alive. that is the other workaround I can think off to solve the 4 seconds load issue.

thanks again!