GrayR
GrayR

Reputation: 1385

Something like R.cache but in RAM

Just wonder if there exists something like R.cache package but working not with hard drive but with RAM instead?

Or maybe there is some hack possible in R, to make R.cache package believe that it uses hard drive, but to store it's cache to virtual drive of some kind in RAM?

I've also found this great question and tried memoise package, but it turned out to be slower then R.cache for my problem, though it works on RAM.

Upvotes: 3

Views: 176

Answers (2)

daroczig
daroczig

Reputation: 28632

You might give pander's evals function a try which has a custom cache engine.

See the above link for details but in short:

  • Enable caching: evalsOptions('cache', TRUE) (default value)
  • You can set a min. eval time in seconds where the results should be cached: evalsOptions('cache.time', 0.1) (default value)
  • Specify where you want to store the cached values and hashes (disk vs. R environment): evalsOptions('cache.mode', 'environment') (default value)

A short example:

> library(pander)

> # first time run
> system.time(evals('sapply(rep(mtcars$hp, 1e3), mean)'))
   user  system elapsed 
 12.269   0.020  12.414 

> # second call
> system.time(evals('sapply(rep(mtcars$hp, 1e3), mean)'))
   user  system elapsed 
  0.003   0.000   0.003 

> # check results any time without recomputing those
> str(evals('sapply(rep(mtcars$hp, 1e3), mean)')[[1]]$result)
 num [1:32000] 110 110 93 110 175 105 245 62 95 123 ...
> str(evals('sapply(rep(mtcars$hp, 1e3), mean)'))
List of 1
 $ :List of 6
  ..$ src   : chr "sapply(rep(mtcars$hp, 1000), mean)"
  ..$ result: num [1:32000] 110 110 93 110 175 105 245 62 95 123 ...
  ..$ output: chr [1:1778] "    [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215 230  66" "   [19]  52  65  97 150 150 245 175  66  91 113 264 175 335 109 110 110  93 110" "   [37] 175 105 245  62  95 123 123 180 180 180 205 215 230  66  52  65  97 150" "   [55] 150 245 175  66  91 113 264 175 335 109 110 110  93 110 175 105 245  62" ...
  ..$ type  : chr "numeric"
  ..$ msg   :List of 3
  .. ..$ messages: NULL
  .. ..$ warnings: NULL
  .. ..$ errors  : NULL
  ..$ stdout: NULL
  ..- attr(*, "class")= chr "evals"

Upvotes: 1

Alex Reynolds
Alex Reynolds

Reputation: 96947

Perhaps you could make a RAM disk and specify that drive as the storage destination for your cache, using R.cache.

Upvotes: 5

Related Questions