asUnsigned: Convert a vector to unsigned (positive) values

View source: R/runTime.R

asUnsignedR Documentation

Convert a vector to unsigned (positive) values

Description

This function is used as a check on numeric values (either numeric or integer vectors) to ensure that the values are all non-negative. This is used when we pass values from R to a native routine that expects unsigned integers or size_t, etc. types. This raises an error or a warning if it finds any element that is negative.

Usage

asUnsigned(value, type, force = FALSE)

Arguments

value

the numeric values

type

the name of the class to which to coerce the val object. This is done first to provide an opportunity to "correct" any values.

force

a logical value. If FALSE and any of the elements of val are negative, the function raises an error. If force is TRUE, we just raise a warning.

Value

The result of coercing val to class specified by type. If any values are negative, the function raises either a warning or an error.

Author(s)

Duncan Temple Lang

See Also

coerce The RGCCTranslationUnit package

Examples

  x = c(0, 1, 2, 0, 1, 2)
  asUnsigned(x, "integer")

  asUnsigned(c(-1, x), "integer", force = TRUE)
  try(asUnsigned(c(-1, x), "integer", force = FALSE))

  setClass("truncatedUnsignedInt", contains = "integer")
  setValidity("truncatedUnsignedInt",
                function(obj)
                  if(!all(obj >= 0))
                      "some values are negative"
                  else
                     TRUE)

  setAs("numeric", "truncatedUnsignedInt",
            function(from) {
               if(any(from < 0))
                   from[from < 0 ] = 0L
               new("truncatedUnsignedInt", from)
            })
              
  asUnsigned(c(-1, x), "truncatedUnsignedInt", force = TRUE)

omegahat/RAutoGenRunTime documentation built on Jan. 12, 2023, 9:19 p.m.