Checks if values are coercible to a given class. Because of inconsistencies
in R's built-in coercion functions (e.g. as.numeric()
warns when it
introduces NAs but as.logical()
doesn't; as.integer()
will silently
remove decimal places from numeric inputs) we check only for the specific
coercions we want to allow, primarily allowing numeric, integer, or logical
values to be considered valid even when the required type is character.
can_coerce(values, class)
values | Vector of values to check |
---|---|
class | Class of interest |
Boolean value; TRUE if values
are coercible to class
, FALSE
otherwise.
This function is mainly in place so that we can automatically allow numeric read lengths, pH values, etc., which are defined as strings in our annotation vocabulary but can reasonably be numbers.
Additionally, this function will return TRUE
if the values are integers and
the desired class is numeric, and will return TRUE
if the values are
numeric but are whole numbers. 2.0
is considered coercible to integer, but
2.1
is not.
It will also allow the following capitalizations of boolean values: true, True, TRUE, false, False, FALSE. These are all treated as valid booleans by Synapse.
This function will not affect validation of enumerated values, regardless of their class. It is only used when validating annotations that have a required type but no enumerated values.
# Not run because function is not exported if (FALSE) { # Coercible: can_coerce(1, "character") can_coerce(TRUE, "character") can_coerce(1L, "character") can_coerce(1L, "numeric") can_coerce(1.0, "integer") # Not coercible: can_coerce("foo", "numeric") can_coerce("foo", "logical") can_coerce(2.5, "integer") }