Skip to content

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

3 messages · Paul Theodor Pyl, Dirk Eddelbuettel, Romain Francois

#
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;
     }
}

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

Cheers,
Paul
#
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.
3 days later
#
Le 01/07/10 14:24, Paul Theodor Pyl a ?crit :
List::operator[] might move the memory around (if you add a new element 
of the list), so the underlying SEXP will change. We have no choice 
about this, this is dictated by the R API. You need to return the list 
to the R level.

Something like:

while( TRUE ){
	 print("running!")
	res <- mycppfunc( l )
	if( ! res$more ) break ;
	l <- res$l
}

And then internally return a list that contains both the actual result 
and whether it needs to continue.

Alternatively, you could manage the loop internally in C++
Adding an element to a list is achieved by essentially creating a new 
list and copy the content of the initial list.

pairlists don't have that problem but they are weird animals on the R side.