Skip to contents

Similar to with(), but with an interface optimized for specifying single values, sequential evaluation, and an argument order suitable for piping. Can be used to avoid cluttering the environment with intermediate values, like a compact local scope. Inspired by the let expression in Haskell.

Usage

let(expr, ...)

Arguments

expr

Expression to evaluate. The input is captured before evaluation in a context modified by assignments in ....

...

Name-value pairs to assign in the evaluation context for expr. Evaluated sequentially (before expr); you can refer to assignments from previous arguments in subsequent ones.

Value

The value of evaluating expr in the modified context.

See also

with() and local() that perform similar context changes.

Examples

let(x = 1, x + x)
#> [1] 2

# Use earlier assignments
let(x = 2, y = x + 3, y * y)
#> [1] 25

# The equivalent local scope
local({
  x <- 2
  y <- x + 3
  y * y
})
#> [1] 25