yeahzx
2010-Feb-20 04:58 UTC
[Rd] how to create a SEXP which could be accessed in embedded R
Hi all, I am not familiar with writing R extensions. In a C program, I want to create a SEXP and access it in embedded R. How to let the embedded engine know there's a new vector? For example, after creating a SEXP, parsing 'ls()' in embedded R and then evaluating, STRSXP returned will contain the name of the SEXP. Any help would be appreciated. Regards, Spiral [[alternative HTML version deleted]]
Romain Francois
2010-Feb-20 09:39 UTC
[Rd] how to create a SEXP which could be accessed in embedded R
On 02/20/2010 05:58 AM, yeahzx wrote:> > Hi all, > > I am not familiar with writing R extensions. In a C program, I want to create a SEXP and access it in embedded R. How to let the embedded engine know there's a new vector? For example, after creating a SEXP, parsing 'ls()' in embedded R and then evaluating, STRSXP returned will contain the name of the SEXP. Any help would be appreciated. > > Regards, > SpiralHi , I'm not sure I understand what you mean. If you want the variable to be in the global environment, you have to assign it there. You can use e.g. defineVar : SEXP x = PROTECT( allocVector( STRSXP, 2 ) ); SET_STRING_ELT( x, 0, Rf_mkChar( "foo" ) ) ; SET_STRING_ELT( x, 1, Rf_mkChar( "bar" ) ) ; defineVar( Rf_install("x"), x, R_GlobalEnv ) ; UNPROTECT(1) ; /* x */ Romain PS: with Rcpp, you can do the same as : using namespace Rcpp; Environment global = Environment::global_env() ; CharacterVector x(2) ; x[0] = "foo" ; x[1] = "bar" ; global["x"] = x ; -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/OIXN : raster images and RImageJ |- http://tr.im/OcQe : Rcpp 0.7.7 `- http://tr.im/O1wO : highlight 0.1-5