to() coerces x to the type of .to, dispatching on the class of .to
to the appropriate to_*() function.
Usage
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'character'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'double'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'data.frame'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'factor'
to(
x,
.to,
...,
levels = NULL,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class '`function`'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'integer'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'logical'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'list'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class '`NULL`'
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# Default S3 method
to(
x,
.to,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)Arguments
- x
The object to stabilize.
- .to
A prototype that determines the target type (e.g.,
integer(),factor(levels = c("a", "b"))).- ...
Arguments passed to methods and on to
to_*()functions.- x_arg
(
character(1)) The name of the object being stabilized to use in error messages. The automatic value will work in most cases, or pass it through from higher-level functions to make error messages clearer in unexported functions.- call
(environment)The execution environment to mention as the source of error messages.- x_class
(
character(1)) The class name of the object being stabilized to use in error messages. Use this if you remove a special class from the object before checking its coercion, but want the error message to match the original class.- levels
(character)The desired factor levels. For factors, a vector'slevelsplay the same role thatallowed_valuesplays for other types: they restrictxto a fixed set of permitted values.
See also
Other character functions:
are_chr_ish(),
specify_chr(),
stabilize_chr(),
stabilize_chr_scalar(),
to_chr(),
to_chr_scalar()
Other double functions:
are_dbl_ish(),
specify_dbl(),
stabilize_dbl(),
stabilize_dbl_scalar(),
to_dbl(),
to_dbl_scalar()
Other integer functions:
are_int_ish(),
specify_int(),
stabilize_int(),
stabilize_int_scalar(),
to_int(),
to_int_scalar()
Other logical functions:
are_lgl_ish(),
specify_lgl(),
stabilize_lgl(),
stabilize_lgl_scalar(),
to_lgl(),
to_lgl_scalar()
Other factor functions:
are_fct_ish(),
specify_fct(),
stabilize_fct(),
stabilize_fct_scalar(),
to_fct(),
to_fct_scalar()
Other function functions:
are_fn_ish(),
to_fn()
Other list functions:
assert_present(),
specify_lst(),
stabilize_lst(),
to_lst()
Other data frame functions:
specify_df(),
stabilize_df(),
to_df()
Examples
to(1L, double())
#> [1] 1
to(1.0, integer())
#> [1] 1
to(TRUE, character())
#> [1] "TRUE"
to("1", integer())
#> [1] 1
to(c("a", "b"), factor(levels = c("a", "b", "c")))
#> [1] a b
#> Levels: a b c
to("mean", mean)
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x5563c9f3f2a0>
#> <environment: namespace:base>