[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-devel
Subject:    Re: [Rd] how to create a SEXP which could be accessed in embedded R
From:       Romain Francois <romain () r-enthusiasts ! com>
Date:       2010-02-20 9:39:23
Message-ID: 4B7FADCB.5020604 () r-enthusiasts ! com
[Download RAW message or body]

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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic