Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-caching ggplot2 output using memoise for use in a web service

For some web services I need to speed up on demand ggplot2 plots as much as possible. With the introduction of memoise, some caching is automatically introduced on the plotting functions, making the second plot significantly faster than the first one. However my web service only draws 1 plot per R session, so this doesn't help by default.

I was wondering if it would be wise/possible/useful to do precaching (e.g. do some calculations onload, before the actual request), or save/load memoise caches to disk for common calls + output.

An example:

> library(ggplot2);
> pdf(tempfile());
> system.time(print(qplot(speed, dist, color=speed, data=cars, geom=c("point", "smooth"))));
   user  system elapsed 
  0.496   0.008   0.512 
> system.time(print(qplot(speed, dist, color=speed, data=cars, geom=c("point", "smooth"))));
   user  system elapsed 
  0.312   0.004   0.322
like image 818
Jeroen Ooms Avatar asked Oct 08 '22 21:10

Jeroen Ooms


1 Answers

AFAIK, the memoise package only supports in-session cache. If you quit an R session, you lose the cache. I'm not sure if the cacheSweave/knitr model helps for your web service, but I believe you have to write the cached results in the disk anyway like these two packages. If the same code is run (verify by MD5), you simply load the cache. You might provide an example so I can know the problem better.

like image 105
Yihui Xie Avatar answered Oct 18 '22 13:10

Yihui Xie