[Rcpp-devel] Modify R object within C++ function that returns bool to indicate success/failure

Dirk Eddelbuettel edd at debian.org
Fri Jul 2 03:12:24 CEST 2010


On 1 July 2010 at 14:24, Paul Theodor Pyl wrote:
| Hi all,
| 
| I have a question concerning the modification of R objects within C++ 
| using Rcpp, what I would like to do is shown in this fantasy-R-session:
|  > l <- list()
|  > while( mycppfunc( l ) ){ print("running!") }
| running!
| running!
| ...
|  > l
| $a
| [1] "Something"
| $b
| ...
| 
| The corresponding c++ function would look like this:
| 
| bool mycppfunc( List l ){
|      some_struct x;
|      if( some_other_function( x ) ){
|          l[x.name] = x.some_member;
|          return true;
|      }else{
|          return false;
|      }
| }

You need a signature

    SEXP mycppfun( SEXP lsexp) {

Use as() to assignt the lsexp to a Rcpp::List object and wrap to return the
boolean.  Otherwise this should work.
 
| where some_other_function is a c++ function that takes the struct and 
| modifies it returning a bool to indicate success/failure.
| 
| Looking at the examples of Rcpp so far I have not found one where an R 
| object is handed to a c++ function that modifies it and returns a bool 
| to indicate success / failure.
| 
| How would I go about this? Could that be done using SEXP's (since they 
| are pointers I would suspect they can be modified 'in-place')

I think so. Because Rcpp works with R's memory management, you should be able
to, say, pass down a vector, grow it and then return the grown vector.

Hope this helps.

-- 
  Regards, Dirk


More information about the Rcpp-devel mailing list