Skip to content

how to create a SEXP which could be accessed in embedded R

2 messages · yeahzx, Romain Francois

#
On 02/20/2010 05:58 AM, yeahzx wrote:
Hi ,

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 ;