[Rcpp-devel] vector<string> -> SEXP

Romain François francoisromain at free.fr
Mon Nov 9 21:02:14 CET 2009


On 11/09/2009 07:23 PM, Dirk Eddelbuettel wrote:
>
> On 9 November 2009 at 17:56, Romain François wrote:
> | Hi,
> |
> | Is there some utility in Rcpp that would convert a vector<string>  into a
> | STRSXP SEXP ?
> |
> | Something like this:
> |
> | SEXP res = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
> | 		
> | 		int i =0;
> | 		for( vector<string>::const_iterator iter = elements.begin(); iter !=
> | elements.end(); ++iter, ++i ) {
> | 			SET_STRING_ELT( res, i, Rf_mkChar( iter->c_str() ) ) ;
> | 		}
> | 		
> | 		UNPROTECT(1) ; /* res */
> | 		return res ;
> |
> | If not, where should it be ? RcppStringVector, or some other class ?
>
>
> Yes, I do that all the time, e.g. when accessing data stores.  You want a
> vector strings back in R, right?
>
> Generally, the idea is to pass 'whatever' into an RcppResultSet as a named
> pair, before converting this RcppResultSet into a list that is returned to
> R.
>
> Look e.g. at RcppResultSet.Rd -- there is an
>
>       rs.add("strings", svec); // vector<string>
>
> followed by
>
>       rl = rs.getReturnList();
>       return(rl);
>
> You should then be able to pick up element 'strings' in R from the result
> list returned to you from .Call().
>
> Does that do the trick?
>
> Dirk
>

I see, thanks. I still think it might be valuable to factor it out as 
maybe a static method of RstringVector. Something like :

static SEXP convertToSEXP( std::vector<std::string> v) ;

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/EAD5 : LondonR slides
|- http://tr.im/BcPw : celebrating R commit #50000
`- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc



More information about the Rcpp-devel mailing list