dot_mutate: Evaluate an arbitrary mutating expression in a magrittr pipe

View source: R/prog_utils.R

dot_mutateR Documentation

Evaluate an arbitrary mutating expression in a magrittr pipe

Description

Evaluate an arbitrary mutating expression in a magrittr pipe

Usage

dot_mutate(x, expr)

Arguments

x

The object to assign into. (Typically this argument is delivered via %\>%().)

expr

The expression to be evaluated with . set to x. Typically this expression modifies . in some way, e.g. by performing an assignment with . on the left-hand side.

Value

The value of x after evaluating expr.

As usual, x, the object being passed in, should be referenced in expr as ..

Note that this function uses the lazyeval package rather than its apparent successor, rlang, because rlang doesn't support interpolating expressions on the left-hand-side of an assignment operation: https://github.com/r-lib/rlang/issues/212.

Examples


library(magrittr)

# Returns the entire list, not just a single element
list(a = 1, b = 2, c = 3) %>%
  dot_mutate(.$d <- 4) %>%
  dot_mutate({ .$e <- 5; .$f <- 6})

# Compare:
list(a = 1, b = 2, c = 3) %>%
  {.$d <- 4} %>%
  print


DarwinAwardWinner/rctutils documentation built on July 22, 2022, 5:19 a.m.