These errors and warnings have custom classes and structures to make testing easier.

stop_incompatible_type(x, y, x_arg = "", y_arg = "", details = NULL,
  ..., message = NULL, .subclass = NULL)

stop_incompatible_cast(x, y, details = NULL, ..., message = NULL,
  .subclass = NULL)

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

maybe_lossy_cast(result, x, to, lossy = NULL, locations = NULL,
  details = NULL, ..., message = NULL, .subclass = NULL,
  .deprecation = FALSE)

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

Arguments

x, y

Vectors

details

Any additional human readable details

..., message, .subclass

Only use these fields when creating a subclass.

result

The result of a potentially lossy cast.

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.

.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().

x_ptype, to_ptype

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

subclass

Use if you want to further customise the class

Lossy cast errors

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.

Examples

# Most of the time, `maybe_lossy_cast()` returns its input normally: maybe_lossy_cast(c("foo", "bar"), NULL, "", lossy = c(FALSE, FALSE))
#> [1] "foo" "bar"
# If `lossy` has any `TRUE`, an error is thrown: try(maybe_lossy_cast(c("foo", "bar"), NULL, "", lossy = c(FALSE, TRUE)))
#> Error : Lossy cast from <NULL> to <character>. #> Backtrace: #> █ #> 1. ├─base::tryCatch(...) #> 2. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 3. │ ├─base:::tryCatchOne(...) #> 4. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 5. │ └─base:::tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> 6. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 7. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 8. ├─base::withCallingHandlers(...) #> 9. ├─base::saveRDS(...) #> 10. ├─base::do.call(...) #> 11. ├─(function (what, args, quote = FALSE, envir = parent.frame()) ... #> 12. └─(function (..., crayon_enabled, crayon_colors, pkgdown_internet) ... #> 13. └─pkgdown::build_site(...) #> 14. └─pkgdown:::build_site_local(...) #> 15. └─pkgdown::build_reference(...) #> 16. └─purrr::map(...) #> 17. └─pkgdown:::.f(.x[[i]], ...) #> 18. └─pkgdown:::data_reference_topic(...) #> 19. ├─pkgdown:::as_data(...) #> 20. └─pkgdown:::as_data.tag_examples(...) #> 21. └─purrr::pmap_chr(...) #> 22. └─pkgdown:::.f(...) #> 23. ├─withr::with_options(...) #> 24. │ └─base::force(code) #> 25. └─evaluate::evaluate(code, env, new_device = TRUE) #> 26. └─evaluate:::evaluate_call(...) #> 27. ├─evaluate:::timing_fn(...) #> 28. ├─evaluate:::handle(...) #> 29. │ └─base::try(f, silent = TRUE) #> 30. │ └─base::tryCatch(...) #> 31. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 32. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 33. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 34. ├─base::withCallingHandlers(...) #> 35. ├─base::withVisible(eval(expr, envir, enclos)) #> 36. └─base::eval(expr, envir, enclos) #> 37. └─base::eval(expr, envir, enclos) #> 38. ├─base::try(...) #> 39. │ └─base::tryCatch(...) #> 40. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 41. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 42. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 43. └─vctrs::maybe_lossy_cast(...) #> 44. ├─base::withRestarts(...) /home/travis/build/r-lib/vctrs/R/conditions.R:164:2 #> 45. │ └─base:::withOneRestart(expr, restarts[[1L]]) #> 46. │ └─base:::doWithOneRestart(return(expr), restart) #> 47. └─vctrs:::stop_lossy_cast(x, to, result, locations = NULL, ...)
# Unless lossy casts are allowed: allow_lossy_cast( maybe_lossy_cast(c("foo", "bar"), NULL, "", lossy = c(FALSE, TRUE)) )
#> [1] "foo" "bar"