# x is a double, but no information is lost
vec_cast(1, integer())
# When information is lost the cast fails
try(vec_cast(c(1, 1.5), integer()))
try(vec_cast(c(1, 2), logical()))
# You can suppress this error and get the partial results
allow_lossy_cast(vec_cast(c(1, 1.5), integer()))
allow_lossy_cast(vec_cast(c(1, 2), logical()))
# By default this suppress all lossy cast errors without
# distinction, but you can be specific about what cast is allowed
# by supplying prototypes
allow_lossy_cast(vec_cast(c(1, 1.5), integer()), to_ptype = integer())
try(allow_lossy_cast(vec_cast(c(1, 2), logical()), to_ptype = integer()))
# No sensible coercion is possible so an error is generated
try(vec_cast(1.5, factor("a")))
# Cast to common type
vec_cast_common(factor("a"), factor(c("a", "b")))
Run the code above in your browser using DataLab