bind: Unpack a list and assign to multiple variables.

Description Usage Arguments Format Details Value Note Author(s) Examples

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

1
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.

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.

Value

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

Note

This will incidentally create a local variable named "bind" in your environment. On the other hand if you have an object already named "bind" and not of class "bind" this method won't be found, so it's merely annoying and not destructive. It's not clear how to avoid this and still use an assignment operator to do the binding. (I could write a simple function, but I strongly prefer there to be a <- anywhere that there is a modification to the environment.)

Nonlocal assignments (<<-) are not supported and will behave as local assignments.

Author(s)

Peter Meilstrup

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#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

crowding/vadr documentation built on May 14, 2019, 11:33 a.m.