reload.or.compute: Compute Unless Previously Saved

View source: R/scriptUtils.R

reload.or.computeR Documentation

Compute Unless Previously Saved

Description

If the designated file does not yet exist, evaluate the expression and save the results in the file. If the file already exists, re-load the results from the file.

Usage

reload.or.compute(filename, expr, objects = NULL,
                  context = parent.frame(), 
                  destination = parent.frame(),
                  force=FALSE, verbose=TRUE)

Arguments

filename

Name of data file. A character string.

expr

R language expression to be evaluated.

objects

Optional character vector of names of objects to be saved in filename after evaluating expr, or names of objects that should be present in filename when loaded.

context

Environment containing objects that are mentioned in expr (other than objects in the global environment).

destination

Environment into which the resulting objects should be assigned.

force

Logical value indicating whether to perform the computation in any case.

verbose

Logical value indicating whether to print a message indicating whether the data were recomputed or reloaded from the file.

Details

This facility is useful for saving, and later re-loading, the results of time-consuming computations. It would typically be used in an R script file or an Sweave document.

If the file called filename does not yet exist, then expr will be evaluated and the results will be saved in filename. The optional argument objects specifies which results should be saved to the file: the default is to save all objects that were created by evaluating the expression.

If the file called filename already exists, then it will be loaded. The optional argument objects specifies the names of objects that should be present in the file; a warning is issued if any of them are missing.

The resulting objects can be assigned into any desired destination. The default behaviour is equivalent to evaluating expr in the current environment.

If force=TRUE then expr will be evaluated (regardless of whether the file already exists or not) and the results will be saved in filename, overwriting any previously-existing file with that name. This is a convenient way to force the code to re-compute everything in an R script file or Sweave document.

Value

Character vector (invisible) giving the names of the objects computed or loaded.

Author(s)

\adrian

and \rolf

Examples

    ## Not run: 
    if(FALSE) {
     reload.or.compute("mydata.rda", {
        x <- very.long.computation()
        y <- 42
      })
   }
   
## End(Not run)

spatstat.core documentation built on May 18, 2022, 9:05 a.m.