unpackAssignOps: Assignment Operators

unpackAssignOpsR Documentation

Assignment Operators

Description

Assign a value to a name, or many values to many names.

Usage

x %<-% value
x %<<-% value
value %->% x
value %->>% x

x %=% value

unpack.assign(x, value, evaluated = TRUE)
unpack.super.assign(x, value, evaluated = TRUE)

Arguments

x

a variable name (possibly quoted), or many variables names.

value

a value to be assigned to x, or many values to be assigned.

evaluated

should x be evaluated?

Details

These operators will assign into the same environments as their non-SPECIAL variants: see <-, <<-, ->, ->>, and =.

x can be a name or an expression defining a part of an object to be replaced (e.g., z[[1]]), or many names or expressions defining parts of objects to be replaced.

These operators add support for using list to unpack a vector of arguments, * to collect a remainder of arguments, and discarding unwanted portions of value, for example:

⁠list(a, b, c) %<-% value⁠

which does something along the lines of:

⁠a <- value[[1]] b <- value[[2]] c <- value[[3]]⁠

You can use * to collect a variable number of arguments, for example:

⁠list(a, b, `*`(args), z) %<-% value⁠

which does something along the lines of:

⁠a <- value[[1]] b <- value[[2]] args <- value[3:length(value) - 1] z <- value[[length(value)]]⁠

You can assign to an empty argument which will discard that portion of value, for example:

⁠list(a, , z) %<-% value⁠

which does something along the lines of:

⁠a <- value[[1]] z <- value[[3]]⁠

You can combine assigning to an empty argument with *, for example:

⁠list(a, `*`(), z) %<-% value⁠

which does something along the lines of:

⁠a <- value[[1]] z <- value[[length(value)]]⁠

You can combine all of these to make some interesting and complicated expressions, for example:

⁠list(a, b[[1]], `*`(args), , x, names(y), attr(z, "test")) %<-% value⁠

which does something along the lines of:

⁠a <- value[[1]] b[[1]] <- value[[2]] args <- value[3:length(value) - 4] x <- value[[length(value) - 2]] names(y) <- value[[length(value) - 1]] attr(z, "test") <- value[[length(value)]]⁠

Value

value. Thus one can use a %<-% b %<-% c %<-% 6.

Examples

`%<-%` <- essentials::`%<-%`


# using 'list' to unpack a vector
list(x, y, z) %<-% 1:3
print(list(x = x, y = y, z = z))


# using '*' to collect vectors
list(a, b, `*`(args), y, z) %<-% 4:14
print(list(a = a, b = b, args = args, y = y, z = z))


# using a missing argument to discard an element
list(a, , b) %<-% 15:17
print(list(a = a, b = b))


# combining '*' with missing argument to discard several elements
list(a, `*`(), z) %<-% 18:28
print(list(a = a, z = z))


# taken from section 'Details'
b <- list(NULL)
y <- 1:26
z <- pi
list(a, b[[1]], `*`(args), , x, names(y), attr(z, "test")) %<-% c(
    list(1:5),
    list(6:10),
    1:10,
    list(letters),
    "example attr"
)
print(list(a = a, b = b, args = args, x = x, y = y, z = z))


# check if an expression would automatically print
list(value, visible) %<-% withVisible(5 + 6)
print(list(value = value, visible = visible))

list(value, visible) %<-% withVisible(invisible(letters[1:5]))
print(list(value = value, visible = visible))

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.