Function, pure R to C

Converts a scalar (length one) logical vector (SEXP) x to a C int.

Arguments

x

a pointer SEXP, referring to an object of type LGLSXP.

Value

A C int value.

Declaration

int asLogical(SEXP x);

In Rinternals.h.

Definition

int asLogical(SEXP x)
{
  return asLogical2(x, /* checking = */ 0, R_NilValue, R_NilValue);
}

In coerce.c.

Examples

# Convert an R length one logical vector to a C int number # Please note that the double backslash in "\n" in Rprintf is only required # because of the inlining of the code here. rlogical_to_cint <- inline::cfunction(c(x = "logical"), ' int x_; x_ = Rf_asLogical(x); Rprintf("x_ is %d\\n", x_); return R_NilValue; ' int x_; x_ = Rf_asLogical(x); Rprintf("x_ is %d\\n", x_); return R_NilValue; ') # NA is mapped to INT_MIN invisible(rlogical_to_cint(NA))
#> x_ is -2147483648
# Empty logical is also mapped to INT_MIN invisible(rlogical_to_cint(NA))
#> x_ is -2147483648
# FALSE is internally mapped to 0 invisible(rlogical_to_cint(FALSE))
#> x_ is 0
# TRUE is internally mapped to 1 invisible(rlogical_to_cint(TRUE))
#> x_ is 1