Learn R Programming

rlang (version 0.1)

vector-coercion: Coerce an object to a base type

Description

These are equivalent to the base functions (e.g. as.logical(), as.list(), etc), but perform coercion rather than conversion. This means they are not generic and will not call S3 conversion methods. They only attempt to coerce the base type of their input. In addition, they have stricter implicit coercion rules and will never attempt any kind of parsing. E.g. they will not try to figure out if a character vector represents integers or booleans. Finally, they have treat attributes consistently, unlike the base R functions: all attributes except names are removed.

Usage

as_logical(x)

as_integer(x)

as_double(x)

as_complex(x)

as_character(x, encoding = NULL)

as_string(x, encoding = NULL)

as_list(x)

Arguments

x

An object to coerce to a base type.

encoding

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

Coercion to logical and numeric atomic vectors

  • To logical vectors: Integer and integerish double vectors. See is_integerish().

  • To integer vectors: Logical and integerish double vectors.

  • To double vectors: Logical and integer vectors.

  • To complex vectors: Logical, integer and double vectors.

Coercion to character vectors

as_character() and as_string() have an optional encoding argument to specify the encoding. R uses this information for internal handling of strings and character vectors. Note that this is only declarative, no encoding conversion is attempted. See as_utf8_character() and as_native_character() for coercing to a character vector and attempt encoding conversion.

See also set_chr_encoding() and mut_utf8_locale() for information about encodings and locales in R, and string() and chr() for other ways of creating strings and character vectors.

Note that only as_string() can coerce symbols to a scalar character vector. This makes the code more explicit and adds an extra type check.

Coercion to lists

as_list() only coerces vector and dictionary types (environments are an example of dictionary type). Unlike base::as.list(), as_list() removes all attributes except names.

Effects of removing attributes

A technical side-effect of removing the attributes of the input is that the underlying objects has to be copied. This has no performance implications in the case of lists because this is a shallow copy: only the list structure is copied, not the contents (see duplicate()). However, be aware that atomic vectors containing large amounts of data will have to be copied.

In general, any attribute modification creates a copy, which is why it is better to avoid using attributes with heavy atomic vectors. Uncopyable objects like environments and symbols are an exception to this rule: in this case, attributes modification happens in place and has side-effects.

Examples

Run this code
# NOT RUN {
# Coercing atomic vectors removes attributes with both base R and rlang:
x <- structure(TRUE, class = "foo", bar = "baz")
as.logical(x)

# But coercing lists preserves attributes in base R but not rlang:
l <- structure(list(TRUE), class = "foo", bar = "baz")
as.list(l)
as_list(l)

# Implicit conversions are performed in base R but not rlang:
as.logical(l)
# }
# NOT RUN {
as_logical(l)
# }
# NOT RUN {
# Conversion methods are bypassed, making the result of the
# coercion more predictable:
as.list.foo <- function(x) "wrong"
as.list(l)
as_list(l)

# The input is never parsed. E.g. character vectors of numbers are
# not converted to numeric types:
as.integer("33")
# }
# NOT RUN {
as_integer("33")
# }
# NOT RUN {

# With base R tools there is no way to convert an environment to a
# list without either triggering method dispatch, or changing the
# original environment. as_list() makes it easy:
x <- structure(as_env(mtcars[1:2]), class = "foobar")
as.list.foobar <- function(x) abort("dont call me")
as_list(x)
# }

Run the code above in your browser using DataLab