[R] Passing characters by .Call

Douglas Bates bates at stat.wisc.edu
Tue Feb 3 18:00:34 CET 2004


> On 2/3/2004 at 5:02 PM Roger Bivand wrote:

> >Yes. First make sure you have your "Writing R Extensions" handy. Next
> >decide whether you are going to use Rdefines.h or Rinternals.h. Then read
> >the chosen header file (in R/includes/). (Rdefines.h includes
> >Rinternals.h). For example line 255 in Rinternals.h gives
> >
> >#define STRING_ELT(x,i)	((SEXP *) DATAPTR(x))[i]
> >
> >I have used incantations like
> >
> >CHAR(STRING_ELT(x, 0)) 
> >
> >where x is a character vector - be prepared to use Rprintf() to check that
> >your assignment works out. 

Another incantation that helps when you are passing a single character
string is
  CHAR(asChar(x))
because asChar will check the type of its argument and coerce it if
necessary.

Similar functions called asInteger, asLogical, and asReal exist for
other types.

Note that these only give you the first element of their argument (or
NA if the argument has length zero or cannot be coerced).




More information about the R-help mailing list