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

Dirk Eddelbuettel edd at debian.org
Mon Nov 9 19:23:34 CET 2009


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

-- 
Three out of two people have difficulties with fractions.


More information about the Rcpp-devel mailing list