R caching with financial data

In the previous post we looked at a simple data caching example which we used to explore the workings of the R-package DataCache.

In this post we continue with this exploration. Instead of just using system time as the datafeed we now use a more real world example of financial data. This is again in preparation for running a custom Shiny server.

So let’s start.

We start by defining the function get_timeseries  to retrieve and fill stock data.

We put this function inside another function datafeed_timeseries that DataCache can understand.

The line

implies that the actual data is saved in the environment under the name of paste0(‘stockdata.’, stock_id) ie if stockid = ‘ALV.DE’ than the data is saved under stockdata.ALV.DE. For another example see the testing example further below.

So here’s the code:

Now we’ll do several tests to see that the cache actually accelerates loading the data, in this set up in fact it is ~ 100 times faster.

Hurray, it works!