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

vctrs-conditions

Custom conditions for vctrs package


Description

These functions are called for their side effect of raising errors and warnings. These conditions have custom classes and structures to make testing easier.

Usage

stop_incompatible_type(
  x,
  y,
  ...,
  x_arg,
  y_arg,
  action = c("combine", "convert"),
  details = NULL,
  message = NULL,
  class = NULL
)

stop_incompatible_cast(
  x,
  to,
  ...,
  x_arg,
  to_arg,
  details = NULL,
  message = NULL,
  class = NULL
)

stop_incompatible_op(
  op,
  x,
  y,
  details = NULL,
  ...,
  message = NULL,
  class = NULL
)

stop_incompatible_size(
  x,
  y,
  x_size,
  y_size,
  ...,
  x_arg,
  y_arg,
  details = NULL,
  message = NULL,
  class = NULL
)

allow_lossy_cast(expr, x_ptype = NULL, to_ptype = NULL)

Arguments

x, y, to

Vectors

..., class

Only use these fields when creating a subclass.

x_arg, y_arg, to_arg

Argument names for x, y, and to. Used in error messages to inform the user about the locations of incompatible types.

action

An option to customize the incompatible type message depending on the context. Errors thrown from vec_ptype2() use "combine" and those thrown from vec_cast() use "convert".

details

Any additional human readable details.

message

An overriding message for the error. details and message are mutually exclusive, supplying both is an error.

x_ptype, to_ptype

Suppress only the casting errors where x or to match these prototypes.

subclass

Use if you want to further customize the class.

Value

stop_incompatible_*() unconditionally raise an error of class "vctrs_error_incompatible_*" and "vctrs_error_incompatible".

Examples

# Most of the time, `maybe_lossy_cast()` returns its input normally:
maybe_lossy_cast(
  c("foo", "bar"),
  NULL,
  "",
  lossy = c(FALSE, FALSE),
  x_arg = "",
  to_arg = ""
)

# If `lossy` has any `TRUE`, an error is thrown:
try(maybe_lossy_cast(
  c("foo", "bar"),
  NULL,
  "",
  lossy = c(FALSE, TRUE),
  x_arg = "",
  to_arg = ""
))

# Unless lossy casts are allowed:
allow_lossy_cast(
  maybe_lossy_cast(
    c("foo", "bar"),
    NULL,
    "",
    lossy = c(FALSE, TRUE),
    x_arg = "",
    to_arg = ""
  )
)

vctrs

Vector Helpers

v0.3.8
MIT + file LICENSE
Authors
Hadley Wickham [aut], Lionel Henry [aut, cre], Davis Vaughan [aut], data.table team [cph] (Radix sort based on data.table's forder() and their contribution to R's order()), RStudio [cph]
Initial release

We don't support your browser anymore

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