Learn R Programming

Hmisc (version 5.1-0)

runifChanged: runifChanged

Description

Re-run Code if an Input Changed

Usage

runifChanged(fun, ..., file = NULL, .print. = TRUE, .inclfun. = TRUE)

Value

the result of running fun

Arguments

fun

the (usually slow) function to run

...

input objects the result of running the function is dependent on

file

file in which to store the result of fun augmented by attributes containing hash digests

.print.

set to TRUE to list which objects changed that neessitated re-running f

.inclfun.

set to FALSE to not include fun in the hash digest, i.e., to not require re-running fun if only fun itself has changed

Author

Frank Harrell

Details

Uses hashCheck to run a function and save the results if specified inputs have changed, otherwise to retrieve results from a file. This makes it easy to see if any objects changed that require re-running a long simulation, and reports on any changes. The file name is taken as the chunk name appended with .rds unless it is given as file=. fun has no arguments. Set .inclfun.=FALSE to not include fun in the hash check (for legacy uses). The typical workflow is as follows.

f <- function(       ) {
# . . . do the real work with multiple function calls ...
}
seed <- 3
set.seed(seed)
w <- runifChanged(f, seed, obj1, obj2, ....)

seed, obj1, obj2, ... are all the objects that f() uses that if changed would give a different result of f(). This can include functions such as those in a package, and f will be re-run if any of the function's code changes. f is also re-run if the code inside f changes. The result of f is stored with saveRDS by default in file named xxx.rds where xxx is the label for the current chunk. To control this use instead file=xxx.rds add the file argument to runifChanged(...). If nothing has changed and the file already exists, the file is read to create the result object (e.g., w above). If f() needs to be run, the hashed input objects are stored as attributes for the result then the enhanced result is written to the file.

See here for examples.