Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

coerce_to

Coerce variable to a different class


Description

Coerce the input to a different class, with a warning. More reliable then as, and supports coercion to multiple classes.

Usage

coerce_to(x, target_class, .xname = get_name_in_parent(x))

Arguments

x

Input to coerce.

target_class

The desired class of x. Multiple values allowed (see note).

.xname

Not intended to be used directly.

Value

The input x after attempted coercion to the target class.

Note

If x does not already have the target class, a warning is given before coercion. The function will try and convert the x to each of the classes given in target_class, in order, until it succeeds or runs out of classes to try. It will first try and convert x using a dedicated as.target_class function if that exists. If it does not exist, or throws an error then coerce_to will try to use as(x, target_class).

See Also

is and as.

Examples

# Numbers can be coerced to characters but not to calls.
dont_stop(coerce_to(1:5, c("call", "character")))

assertive.base

A Lightweight Core of the 'assertive' Package

v0.0-9
GPL (>= 3)
Authors
Richard Cotton [aut, cre], Sunkyu Choi [trl], Ivanka Skakun [trl], Gergely Dar<c3><b3>czi [trl], Anton Antonov [trl], Hisham Ben Hamidane [trl], Anja Billing [trl], Aditya Bhagwat [trl], Rasmus B<c3><a5><c3><a5>th [trl], Mine Cetinkaya-Rundel [trl], Aspasia Chatziefthymiou [trl]
Initial release
2021-02-07

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.