Learn R Programming

favr (version 1.0.0)

favr_casting_recycling_helpers: favr casting and recycling helpers

Description

These functions signal to favr functions to undergo casting, lossy casting, and/or recycling. Each can only be used wihtin calls to specific favr functions and will error if used outside them. Specifically:

  • lossy(): used within cast_if_not() for lossy casting.

  • cast(): used within enforce() and schema() for casting.

  • recycle(): used within enforce() and schema() for recycling.

  • coerce(): used within enforce() and schema() for casting and recycling.

Usage

lossy(x)

cast(x, lossy = FALSE)

recycle(x)

coerce(type = NULL, size = NULL, lossy = FALSE)

Value

No return value, called for side effects only. Will error if called outside of a favr calling context (see Description and Examples).

Arguments

x

input to be lossily casted for lossy(), object of type to cast to for cast(), or scalar integerish value to recycle to for recycle().

lossy

logical, TRUE or FALSE.

type

object of type to cast to for coerce().

size

scalar integerish value to recycle to.

Details

These functions add attributes and/or a class to their inputs that signal transformations to occur within the favr caller.

Examples

Run this code
try(cast(10)) # errors outside of favr calling context

x <- 1.5
cast_if_not(x = lossy(integer()))
class(x) # integer

enforce(x ~ list(cast(double()), recycle(5)))
class(x) # numeric
length(x) # 5

x <- 1.5
enforce(x ~ coerce(type = integer(), size = 5, lossy = TRUE))
class(x) # integer
length(x) # 5

Run the code above in your browser using DataLab