Description Arguments Value Declaration Definition See Also Examples
Coerce an R object to a scalar string, i.e., to an R object of type
CHARSXP
. This object type should only be used internally, i.e.,
it should not be returned back to R.
x |
a pointer |
A pointer SEXP
, referring to an object of type
CHARSXP
.
1 |
In Rinternals.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | SEXP asChar(SEXP x)
{
if (isVectorAtomic(x) && XLENGTH(x) >= 1) {
int w, d, e, wi, di, ei;
char buf[MAXELTSIZE]; /* Probably 100 would suffice */
switch (TYPEOF(x)) {
case LGLSXP:
if (LOGICAL(x)[0] == NA_LOGICAL)
return NA_STRING;
if (LOGICAL(x)[0])
sprintf(buf, "TRUE");
else
sprintf(buf, "FALSE");
return mkChar(buf);
case INTSXP:
if (INTEGER(x)[0] == NA_INTEGER)
return NA_STRING;
snprintf(buf, MAXELTSIZE, "%d", INTEGER(x)[0]);
return mkChar(buf);
case REALSXP:
PrintDefaults();
formatReal(REAL(x), 1, &w, &d, &e, 0);
return mkChar(EncodeReal0(REAL(x)[0], w, d, e, OutDec));
case CPLXSXP:
PrintDefaults();
formatComplex(COMPLEX(x), 1, &w, &d, &e, &wi, &di, &ei, 0);
return mkChar(EncodeComplex(COMPLEX(x)[0], w, d, e, wi, di, ei, OutDec));
case STRSXP:
return STRING_ELT(x, 0);
default:
return NA_STRING;
}
} else if(TYPEOF(x) == CHARSXP) {
return x;
} else if(TYPEOF(x) == SYMSXP)
return PRINTNAME(x);
return NA_STRING;
}
|
In util.c.
R_CHAR
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # Coerces an R object to an object of type CHARSXP
print_char <- inline::cfunction(c(x = "any"),
' const char * x_;
x_ = R_CHAR(Rf_asChar(x));
Rprintf("%s\\n", x_);
return R_NilValue;
')
# Convert from STRSXP to CHARSXP
invisible(print_char("coal"))
invisible(print_char(c("charcoal", "coal")))
invisible(print_char(NA_character_))
# Convert from LGLSXP to CHARSXP
invisible(print_char(TRUE))
invisible(print_char(FALSE))
invisible(print_char(NA))
# Convert from INTSXP to CHARSXP
invisible(print_char(-1L))
invisible(print_char(1L))
invisible(print_char(NA_integer_))
# Convert from REALSXP to CHARSXP
invisible(print_char(1.0))
invisible(print_char(-1))
invisible(print_char(NA_real_))
# Convert from CPLXSXP to CHARSXP
invisible(print_char(2+5i))
# Convert from SYMSXP to CHARSXP
invisible(print_char(as.symbol('a')))
# Coercion from other object types to CHARSXP
# results in NA:
# From VECSXP to CHARSXP
invisible(print_char(list(a = 'b')))
# From ENVSXP to CHARSXP
invisible(print_char(.GlobalEnv))
# From CLOSXP to CHARSXP
invisible(print_char(function() {}))
# From EXPRSXP to CHARSXP
invisible(print_char(expression()))
# From LANGSXP to CHARSXP
invisible(print_char(~ 2 + x))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.