Learn R Programming

rlang (version 0.1)

vector-construction: Create vectors

Description

The atomic vector constructors are equivalent to c() but allow you to be more explicit about the output type. Implicit coercions (e.g. from integer to logical) follow the rules described in vector-coercion. In addition, all constructors support splicing: if you supply bare lists or explicitly spliced lists, their contents are spliced into the output vectors (see below for details). ll() is a list constructor similar to base::list() but with splicing semantics.

Usage

lgl(...)

int(...)

dbl(...)

cpl(...)

chr(..., .encoding = NULL)

bytes(...)

ll(...)

Arguments

...

Components of the new vector. Bare lists and explicitly spliced lists are spliced.

.encoding

If non-null, passed to set_chr_encoding() to add an encoding mark. This is only declarative, no encoding conversion is performed.

Splicing

Splicing is an operation similar to flattening one level of nested lists, e.g. with base::unlist(x, recursive = FALSE) or purrr::flatten(). ll() returns its arguments as a list, just like list() would, but inner lists qualifying for splicing are flattened. That is, their contents are embedded in the surrounding list. Similarly, chr() concatenates its arguments and returns them as a single character vector, but inner lists are flattened before concatenation.

Whether an inner list qualifies for splicing is determined by the type of splicing semantics. All the atomic constructors like chr() have list splicing semantics: bare lists and explicitly spliced lists are spliced.

There are two list constructors with different splicing semantics. ll() only splices lists explicitly marked with splice().

See Also

ll()

Examples

Run this code
# NOT RUN {
# These constructors are like a typed version of c():
c(TRUE, FALSE)
lgl(TRUE, FALSE)

# They follow a restricted set of coercion rules:
int(TRUE, FALSE, 20)

# Lists can be spliced:
dbl(10, list(1, 2L), TRUE)


# They splice names a bit differently than c(). The latter
# automatically composes inner and outer names:
c(a = c(A = 10), b = c(B = 20, C = 30))

# On the other hand, rlang's ctors use the inner names and issue a
# warning to inform the user that the outer names are ignored:
dbl(a = c(A = 10), b = c(B = 20, C = 30))
dbl(a = c(1, 2))

# As an exception, it is allowed to provide an outer name when the
# inner vector is an unnamed scalar atomic:
dbl(a = 1)

# Spliced lists behave the same way:
dbl(list(a = 1))
dbl(list(a = c(A = 1)))

# bytes() accepts integerish inputs
bytes(1:10)
bytes(0x01, 0xff, c(0x03, 0x05), list(10, 20, 30L))

# The list constructor has explicit splicing semantics:
ll(1, list(2))

# Note that explicitly spliced lists are always spliced:
ll(!!! list(1, 2))
# }

Run the code above in your browser using DataLab