Skip to contents

to() converts 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),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'character'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'double'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'Date'
to(
  x,
  .to,
  ...,
  tz = resolve_tz(.to),
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'data.frame'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'factor'
to(
  x,
  .to,
  ...,
  levels = NULL,
  ordered = is.ordered(x) || is.ordered(.to),
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class '`function`'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'integer'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'logical'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'list'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'array'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class '`NULL`'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'hms'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'Period'
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# S3 method for class 'POSIXct'
to(
  x,
  .to,
  ...,
  tz = resolve_tz(.to),
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  call = caller_env(),
  x_class = object_type(x)
)

# Default S3 method
to(
  x,
  .to,
  ...,
  x_arg = caller_arg(x),
  to_arg = caller_arg(.to),
  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.

to_arg

(character(1) or NULL) The name of the object that x is being converted to match, for use in error messages. When NULL, error messages describe only x. When supplied, conversion failure messages name both x_arg and to_arg.

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 conversion, but want the error message to match the original class.

tz

(character(1)) The time zone to use when .to is a base::POSIXct or base::Date. Must be a value from OlsonNames() or "" (your local time zone). Defaults to the time zone of .to, or your local time zone if .to doesn't name one (see resolve_tz()).

levels

(character) The desired factor levels. For factors, a vector's levels play the same role that allowed_values plays for other types: they restrict x to a fixed set of permitted values.

ordered

(logical(1)) Should the result be an ordered factor?

Value

x converted to the type of .to, or an error condition with classes <stbl-error>, <stbl-condition>, <rlang_error>, <error>, <condition>, and a specific class by failure mode:

  • The failure classes documented for the to_*() function corresponding to the class of .to (for example to_int() for .to = integer()).

  • <stbl-error-coerce-*> when the class of .to has no corresponding to_*() function and vctrs::vec_cast() cannot converted x to .to.

Details

When the class of .to does not have a corresponding to_*() function, to() falls back to vctrs::vec_cast(). Arguments in ... are passed to to_*() functions but are ignored by the vctrs::vec_cast() fallback.

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: 0x555ee4255528>
#> <environment: namespace:base>
to("2024-01-01", as.Date("2024-01-01"))
#> [1] "2024-01-01"
to("2024-01-01T12:00:00Z", as.POSIXct("2024-01-01", tz = "UTC"))
#> [1] "2024-01-01 12:00:00 UTC"