[R] R strings from C

Duncan Temple Lang duncan at jessie.research.bell-labs.com
Wed Dec 19 21:54:54 CET 2001


Hi,

R does not have scalar/individual values but vectors.
So, in a call to an R function,
  foo("x", "abc")
both arguments are "character vectors" rather than strings.
So they may actually be a collection of strings.

In C code, the elements of the  character vectors are accessed 
via 
 STRING_ELT(vector, elementIndex)
and
 SET_STRING_ELT(vector, elementIndex)
for getting and setting the value.

There is one more layer. Each element is itself yet another SEXP, this
time of type CHARSXP. And you can fetch the low-level C char * from this
using the macro CHAR() (or R_CHAR()).
So to get the C string for the first element in the character vector
you would use

  CHAR(STRING_ELT(file, 0))

Of course, when printing it in C, you need to use %s, not %d
as you have in your code.

R_CHAR is in fact a routine/macro, and not a type.

BTW, character vectors are stored very differently in S4/S-Plus.  (I
write code that runs in both systems so I have a collection of macros,
some of which are in Rdefines.h, that hide the differences.)

 D.

Ott Toomet wrote:
> Hi,
> 
> I am trying to study R internal behaviour.  So long, I have not
> succeeded to access the value of R strings from C.
> 
> I use:
> 
> void salvesta_tabel(
> 		    SEXP data_frame,
> 		    SEXP file
> 		    )
> {
>   printf( "nimi %d\n", (R_CHAR)( file));
> }
> 
> and from the R side:
> 
> salvesta.tabel <-
>   function (x, file = "") {
>     .Call( "salvesta_tabel", x, file)
>   }
> 
> When calling from R as
> 
> salvesta.tabel( x, "file")
> 
> R always crashes while printing the %d part.  So I assume that the
> function R_CHAR is not the right one to access the value of the
> variable file, I have experimented several other ways but so long
> unsuccessfully, both Venables & Ripley ,S-programming' and ,Writing R
> extensions' seem not to touch that issue.
> 
> Any suggestions?
> 
> And thanks in advance,
> 
> Ott Toomet
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
_______________________________________________________________

Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
         http://cm.bell-labs.com/stat/duncan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list