R/ensure_integer.R

Defines functions ensure_integer

ensure_integer <- function(x, on_failure = c("error", "warn")) {
  if (is.integer(x)) {
    return(x)
  }
  if (is.double(x)) {
    # 0 if all ok, otherwise position
    int_status <- .Call("Cwhich_isnt_integerish", x, PACKAGE = packageName)
    if (int_status) {
      switch(on_failure[1],
             error = stop("`", vname(x), "` was not integerish at position ",
                          int_status, "."),
             warn = warning("`", vname(x), "` was not integerish at position ",
                            int_status, "."))

    }
    return(as.integer(x))
  }
  stop("`", vname(x), "` was type ", typeof(x), ", but must be integer.")
}

Try the hutilscpp package in your browser

Any scripts or data that you put into this service are public.

hutilscpp documentation built on Oct. 11, 2023, 9:06 a.m.