unpack | R Documentation |
Unpacks or binds values into the calling environment. Uses bquote
escaping.
NULL is a special case that is unpacked to all targets. NA targets are skipped.
All non-NA target names must be unique.
unpack(wrapr_private_value, ...)
wrapr_private_value |
list of values to copy |
... |
argument names to write to |
Note: when using []<-
notation, a reference to the unpacker object is written into the unpacking environment as a side-effect
of the implied array assignment. :=
assigment does not have this side-effect.
Array-assign form can not use the names: .
, wrapr_private_self
, value
, or unpack
.
Function form can not use the names: .
or wrapr_private_value
.
For more details please see here https://win-vector.com/2020/01/20/unpack-your-values-in-r/.
Related work includes Python
tuple unpacking, zeallot
's arrow, and vadr::bind
.
value passed in (invisible)
pack
# named unpacking
# looks like assignment: DESTINATION = NAME_VALUE_USING
d <- data.frame(x = 1:2,
g=c('test', 'train'),
stringsAsFactors = FALSE)
unpack[train_set = train, test_set = test] := split(d, d$g)
# train_set and test_set now correctly split
print(train_set)
print(test_set)
rm(list = c('train_set', 'test_set'))
# named unpacking NEWNAME = OLDNAME implicit form
# values are matched by name, not index
unpack[train, test] := split(d, d$g)
print(train)
print(test)
rm(list = c('train', 'test'))
# function version
unpack(split(d, d$g), train, test)
print(train)
print(test)
rm(list = c('train', 'test'))
# pipe version
split(d, d$g) %.>% unpack(., train, test)
print(train)
print(test)
rm(list = c('train', 'test'))
# Note: above is wrapr dot-pipe, piping does not currently work with
# magrittr pipe due to magrittr's introduction of temporary
# intermediate environments during evaluation.
# bquote example
train_col_name <- 'train'
test_col_name <- 'test'
unpack(split(d, d$g), train = .(train_col_name), test = .(test_col_name))
print(train)
print(test)
rm(list = c('train', 'test'))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.