Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache data in shiny server?

Tags:

I am using R to deploy an app over the web, but the URL from which my app takes data is where my app takes time. Is it possible to cache that data? I tried to install the packages memoise, R.cache and a few more that were all unsupported by the server.

like image 721
Paras Sheth Avatar asked Mar 16 '16 04:03

Paras Sheth


People also ask

How do you load data on the shiny app?

Data is loaded into shinyapps.io in a few different ways: The simplest way to get data into an application is by uploading a CSV, RData or other data file directly with the application source code. In this model, the application author includes the data files as part of the application.

Why is the shiny app so slow?

There are many reasons why a shiny app runs slower than expected. The most common reason is the Shiny app code has not been optimized. You can use the profvis package to help you understand how R spends its time. You also might want to make sure your server is large enough to host your apps.

How do I use shiny Profvis?

To do this, simply execute the runApp() command inside of profvis . For instance, we can run one of shiny's built-in examples using the runExample command (which is a wrapper for runApp ). Your Shiny application will launch, and after interacting and closing the app a profile will be generated.


1 Answers

I recommend trying the DataCache package by Jason Bryer. The package is available through GitHub and I was successful in using it today for a Shiny app that I am developing.

The main function from that package is data.cache. You will need to a define a function that generates a list of objects that you want to cache, and then pass the function you define as an argument to data.cache. I also recommend setting the cache.name parameter of data.cache if you intend on caching more than one list of objects in your application.

For example:

DataCache::data.cache(
  function(){
    list(
      normal_random_numbers = rnorm(10),
      uniform_random_numbers = runif(10)
    )
  },
  cache.name = 'my_random_numbers'
)

The above code creates two objects in the local environment, normal_random_numbers and uniform_random_numbers, as well as caches these to the file system. When you run this code again, the cached copies of these objects will be used rather than being regenerated - unless of course the cache expires. The frequency parameter of data.cache is used to set the expiry of the cache, which is set to daily by default.

If you are running the application under Windows then use this slightly modified version of the package. This is to address--- a bug that is apparently due to the cache filename being incompatible with the Windows file system.

like image 72
johannux Avatar answered Oct 12 '22 12:10

johannux