assign_into: Assign into complex sub-expressions and return the whole...

View source: R/prog_utils.R

assign_intoR Documentation

Assign into complex sub-expressions and return the whole object

Description

Assign into complex sub-expressions and return the whole object

Usage

assign_into(x, expr, value)

Arguments

x

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

expr

The left hand side of the assignment operation to be performed on x.

value

The right hand side of the assignment operation to be performed.

Value

The value of x after performing the assignment.

As usual, x, the object being passed in, should be referenced in both expr and value as .. In fact, expr *must* contain at least one ., or else the object will not be modified. (This is currently not checked.)

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.

See Also

%\>% from the magrittr package.

Examples


library(magrittr)

# Returns the entire list, not just the value of x
list(a = 1, b = 2, c = 3) %>% assign_into(.$x, 5)

## Not run: 
# A more complex example of how this might be used in the middle of
# a pipeline. Imagine that x is a list of SummarizedExperiment
# objects, and for some reason one of the values in one of the
# assays in one of the objects is wrong and needs to be modified.
x %>% assign_into(assays(.[[1]])$counts[3,5], 45) %>% ...

## End(Not run)


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