loadCache              package:R.cache              R Documentation

_L_o_a_d_s _d_a_t_a _f_r_o_m _f_i_l_e _c_a_c_h_e

_D_e_s_c_r_i_p_t_i_o_n:

     Loads data from file cache, which is unique for an optional key
     object.

_U_s_a_g_e:

     ## Default S3 method:
     loadCache(key=NULL, sources=NULL, suffix=".Rcache", removeOldCache=TRUE, pathname=NULL, dirs=NULL, ...)

_A_r_g_u_m_e_n_t_s:

     key: An optional object from which a hexadecimal hash code will be
          generated and appended to the filename.

 sources: Optional source objects.  If the cache object has a timestamp
          older than one of the source objects, it will be ignored and
          removed.

  suffix: A 'character' string to be appended to the end of the
          filename.

removeOldCache: If 'TRUE' and the cache is older than the 'sources',
          the cache file is removed, otherwise not.

pathname: The pathname to the cache file.  If specified, arguments
          'key' and 'suffix' are ignored.  Note that this is only
          needed in order to read a cache file for which the key is
          unknown, for instance, in order to investigate an unknown
          cache file.

    dirs: A 'character' 'vector' constituting the path to the cache
          subdirectory to be used. If 'NULL', the root path is used.

     ...: Additional argument passed to 'load'().

_D_e_t_a_i_l_s:

     The hash code calculated from the 'key' object is a 32 characters
     long hexadecimal MD5 hash code.  For more details, see the
     _digest_ package.

_V_a_l_u_e:

     Returns an R object or 'NULL', if cache does not exist.

_R_e_q_u_i_r_e_m_e_n_t_s:

     To make use of the 'key' argument, the _digest_ package (available
     on CRAN) must be installed, otherwise an error is generated. That
     package is not required when 'key==NULL'.

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_S_e_e _A_l_s_o:

     'saveCache'(). The 'cache' method in Bioconductor.

_E_x_a_m_p_l_e_s:

     simulate <- function(mean, sd) {
       # 1. Try to load cached data, if already generated
       key <- list(mean, sd)
       data <- loadCache(key)
       if (!is.null(data)) {
         cat("Loaded cached data\n")
         return(data);
       }

       # 2. If not available, generate it.
       cat("Generating data from scratch...")
       data <- rnorm(1000, mean=mean, sd=sd)
       Sys.sleep(1)             # Emulate slow algorithm
       cat("ok\n")
       saveCache(data, key=key, comment="simulate()")

       data;
     }

     data <- simulate(2.3, 3.0)
     data <- simulate(2.3, 3.5)
     data <- simulate(2.3, 3.0) # Will load cached data

     # Clean up
     file.remove(findCache(key=list(2.3,3.0)))
     file.remove(findCache(key=list(2.3,3.5)))

