From romain at r-enthusiasts.com  Sat Feb 20 10:39:23 2010
From: romain at r-enthusiasts.com (Romain Francois)
Date: Sat, 20 Feb 2010 10:39:23 +0100
Subject: [Rd] how to create a SEXP which could be accessed in embedded R
In-Reply-To: <15308325.48ac.126e9bd5f7d.Coremail.yeahzx@yeah.net>
References: <15308325.48ac.126e9bd5f7d.Coremail.yeahzx@yeah.net>
Message-ID: <4B7FADCB.5020604@r-enthusiasts.com>

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,
> Spiral

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 ;

-- 
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



[Rd] how to create a SEXP which could be accessed in embedded R
Romain Francois