Coerce an object to a base type

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.

Arguments

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.

Coercion to lists

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.

# Implicit conversions are performed in base R but not rlang:as.logical(l)

#> [1] TRUE

not_run({
as_logical(l)
})
# Conversion methods are bypassed, making the result of the# coercion more predictable:as.list.foo<-function(x) "wrong"as.list(l)

#> [1] "wrong"

as_list(l)

#> [[1]]
#> [1] TRUE
#>

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

#> [1] 33

not_run({
as_integer("33")
})
# 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)