Monday, September 19, 2016

Using the R.cache package

# RStudio instance #1 - the cache saving test piece
# Load R.cache library, set working and cache directories
library("R.cache")
setCacheRootPath(path="C:\\Data Study\\Class 5 Reproducible Research\\Week 4")
setwd("C:\\Data Study\\Class 5 Reproducible Research\\Assignment 2")

# Load a large data file that takes a while to load
data1 <- ata="" bzfile="" div="" read.csv="" tormdata.csv.bz2="">

# Cache the loaded data object using key ABC
# Creates file d125f08eb1bf24d4c1e2b38b35d3362b.Rcache (389,576 KB)
key <- div="" list="">
saveCache(data1,key)
> nrow(data1)
[1] 902297
> ncol(data1)
[1]

                   # RStudio instance #2 (separate UI, process) - the cache reading test piece
                   # Load R.cache library, set working and cache directories
                   library("R.cache")
                   setCacheRootPath(path="C:\\Data Study\\Class 5 Reproducible Research\\Week 4")
                   setwd("C:\\Data Study\\Class 5 Reproducible Research\\Assignment 2")

                   # Load the large cached data object - much faster than original load!
                   key <- b="" list="">
                   dataX <- b="" key="" loadcache="">
                    > nrow(dataX)
                    [1] 902297
                    > ncol(dataX)
                    [1] 37

# Cache the datamod() function  itself
# Creates file f4971f5a1be44fc8fddb87235d45aaaa.Rcache (2 KB)
key <- div="" list="">
saveCache(datamod,key)

                   # Load the datamod() function 
                   key <- b="" list="">
                   dataX <- b="" key="" loadcache="">
                   > dataX(4)
                   [1] 12

# Cache the result of a datamod function call
# Creates file 1b2526c00d8ed8a42700fc0fb39c1b1b.Rcache (1 KB)
key <- div="" list="">
saveCache(datamod(6),key)

                   # Load the datamod() function return value 
                   key <- b="" list="">
                   dataX <- b="" key="" loadcache="">
                   > dataX
                   [1] 18

# Small sample function
datamod <- div="" function="" myarg="">
{
  myarg * 3
}

No comments:

Post a Comment