set: Assign values only when needed

Description Usage Arguments Details Value See Also Examples

View source: R/coding.R

Description

A helper function for avoiding long-running computations if the object to be generated is either already present or can be read from an Rda file.

Usage

1
2
  set(name, expr, template = "%s.Rda",
    env = parent.frame(), inherits = TRUE)

Arguments

name

Character scalar indicating the name of an object to be assigned.

expr

Expression that will only be evaluated if name does not yet exist and, optionally, cannot be read from an Rda file.

template

Character scalar with a template as used by sprintf from the base package, with the placeholder, if any, referring to name. Set this to an empty object to turn of the Rda file mechanism.

env

Passed as envir argument to assign.

inherits

Passed as inherits argument to assign.

Details

If the Rda file name has no directory component, it is assumed to be located in the directory given by getOption("rda_store") and, if this does not exist, in the directory given by getwd().

Value

An invisible returned integer code indicating what has been done.

0

The object already existed, nothing was done.

1

The result of expr was assigned to the object, the Rda file mechanism was not used.

2

The given Rda file was found and its content assigned to the object.

3

The given Rda file was not found. The result of expr was assigned to the object and was also stored in the given Rda file.

See Also

base::assign base::readRDS base::saveRDS

Other coding-functions: L, LL, assert, case, check, collect, contains, flatten, listing, map_names, map_values, must, sql, unnest

Examples

1
2
3
4
5
6
7
the_answer <- function() {
  print("answer requested")
  42L
}
set("answer", the_answer(), NULL) # prints 'answer requested'
set("answer", the_answer(), NULL) # does not print
answer # 42

pkgutils documentation built on May 2, 2019, 5:49 p.m.

Related to set in pkgutils...