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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 19 Dec 2001, 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.What you are passing is a character vector, not a string. So you need (untested) #include <R.h> #include <Rinternals.h> SEXP salvesta_tabel(SEXP data_frame, SEXP file) { printf( "nimi %s\n", CHAR(STRING_ELT(file, 0))); } to print the first element of a character vector. Note, you have to return a SEXP, and I use %s to print a string, not %d. I would add some testing (isString(file) && length(file) >= 1 would be a good start).> > 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.There *are* examples in `Writing R Extensions': search for STRING_ELT. Fortunately there are not in `S-programming' since the way to do this has changed since that was written. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Seemingly Similar Threads
- accessing factor levels from C
- another fix for R crashes under enable-strict-barrier, lto, trunk@72156
- another fix for R crashes under enable-strict-barrier, lto, trunk@72156
- [LLVMdev] [PATCH]: Add SparseBitmap implementation
- [LLVMdev] [PATCH]: Add SparseBitmap implementation