vadr (version 0.01)

bind: Unpack a list and assign to multiple variables.

Description

This is a "destructuring bind" for R. It can be used to unpack structured lists into different variables, or achieve the effect of multiple return values from a function.

Usage

bind[key=varName, ...] <- list(key=value, ...)

Arguments

...
a list of assignments, key on left, target variable on right. That is, bind[a=x] <- c(a=1) creates a variable named x, not a. It is somewhat counterintuitive but this is the only way that matches R's argument binding syntax.

Value

a "bind" object, since it is invoked via a subset on "bind".

Format

N/A

Details

Element to variable matching should match R's argument binding rules, with the modification that arguments to the right of the ... will be matched positionally to elements at the end of the unpacked sequence. Calls to bind() can be nested to unpack nested structures.

You may leave an argument blank as in bind[, skipKey=, ...=rest] <- seq to skip an element. (Here the first element of seq and the one tagged "skipKey" are both skipped and the rest are gathered in the output variable rest.)

Note that the assigned-to variable is on the right side of each = in the argument list. This is admittedly awkward but is the best way to remain consistent with R's argument-binding semantics.

Examples

Run this code
#match by position
bind[x, y] <- c("foo", "bar")

#match by name
bind[a=x, b=y] <- c(b="bar", a="foo")

# one often wants to unpack the first and/or last, and rest of a list.
bind[first, ...=rest, last] <- letters

record <- list("Marilyn", "Monroe", dob=list("June", 1, 1926),
               profession="film star", "born Norma Jean Baker",
               donotuse="garbage", "1947 California Artichoke Queen",
               list("August", 5, 1962))
bind[first, last,
     dob=bind[month, day, year],
     donotuse=, ...=notes, death] <- record

Run the code above in your browser using DataLab