Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

local_bindings

Temporarily change bindings of an environment


Description

  • local_bindings() temporarily changes bindings in .env (which is by default the caller environment). The bindings are reset to their original values when the current frame (or an arbitrary one if you specify .frame) goes out of scope.

  • with_bindings() evaluates expr with temporary bindings. When with_bindings() returns, bindings are reset to their original values. It is a simple wrapper around local_bindings().

Usage

local_bindings(..., .env = .frame, .frame = caller_env())

with_bindings(.expr, ..., .env = caller_env())

Arguments

...

Pairs of names and values. These dots support splicing (with value semantics) and name unquoting.

.env

An environment.

.frame

The frame environment that determines the scope of the temporary bindings. When that frame is popped from the call stack, bindings are switched back to their original values.

.expr

An expression to evaluate with temporary bindings.

Value

local_bindings() returns the values of old bindings invisibly; with_bindings() returns the value of expr.

Examples

foo <- "foo"
bar <- "bar"

# `foo` will be temporarily rebinded while executing `expr`
with_bindings(paste(foo, bar), foo = "rebinded")
paste(foo, bar)

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.