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

maybe_lossy_cast

Lossy cast error


Description

[Experimental]

By default, lossy casts are an error. Use allow_lossy_cast() to silence these errors and continue with the partial results. In this case the lost values are typically set to NA or to a lower value resolution, depending on the type of cast.

Lossy cast errors are thrown by maybe_lossy_cast(). Unlike functions prefixed with stop_, maybe_lossy_cast() usually returns a result. If a lossy cast is detected, it throws an error, unless it's been wrapped in allow_lossy_cast(). In that case, it returns the result silently.

Usage

maybe_lossy_cast(
  result,
  x,
  to,
  lossy = NULL,
  locations = NULL,
  ...,
  loss_type = c("precision", "generality"),
  x_arg,
  to_arg,
  details = NULL,
  message = NULL,
  class = NULL,
  .deprecation = FALSE
)

Arguments

result

The result of a potentially lossy cast.

x

Vectors

to

Type to cast to.

lossy

A logical vector indicating which elements of result were lossy.

Can also be a single TRUE, but note that locations picks up locations from this vector by default. In this case, supply your own location vector, possibly empty.

locations

An optional integer vector giving the locations where x lost information.

...

Only use these fields when creating a subclass.

loss_type

The kind of lossy cast to be mentioned in error messages. Can be loss of precision (for instance from double to integer) or loss of generality (from character to factor).

x_arg

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

to_arg

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

details

Any additional human readable details.

message

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

class

Only use these fields when creating a subclass.

.deprecation

If TRUE, the error is downgraded to a deprecation warning. This is useful for transitioning your class to a stricter conversion scheme. The warning advises your users to wrap their code with allow_lossy_cast().


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.