[Rcpp-devel] Problem with push_back to a list

Dirk Eddelbuettel edd at debian.org
Tue Oct 25 22:34:57 CEST 2011


On 25 October 2011 at 12:46, Michael Hannon wrote:
| Greetings.  I need to return a list of initially-indeterminate length from
| C++.  I found the following note from Romain:
| 
|     http://article.gmane.org/gmane.comp.lang.r.rcpp/69/match=push%5fback+list
| 
| that seemed to solve the problem.
| 
| I made what I thought were some minor tweaks to the example (using cxxfunction
| instead of cfunction, for instance) and tried to run it on my system.
| 
| The example works fine if I leave out the push of the character string:
| 
|     myList.push_back(Rcpp::Named ("foo", "bar"));
| 
| but fails with an obscure (to me) compilation error when I include it.
| 
| I've appended first the R code and after that the log of an R session in which
| I tried to run the code.
| 
| I might add that I've tried some variations of the problematic line but have
| always gotten compile errors.  Here are a couple of examples:
| 
|     myList.push_back(Named ("foo", "bar"));
|     myList.push_back(Rcpp::Named ("foo", 10));
| 
| Thanks for any help you can provide.

You were very, very close to this existing example from the unit test file
runit.Vector.R (which, as Lists are GenericVector objects, also contains
lists):

                  ,"list_push_back"=list(
                   signature(x = "list"),
                   'List list(x) ;
	            list.push_back( 10 ) ;
	            list.push_back( "bar", "foo" ) ;
	            return list ;
	           ')

So with that it works if I write your example like this:

-----------------------------------------------------------------------------
library(inline)

code <- '
     Rcpp::List myList(rList);
     myList.push_back(10);
     myList.push_back("foo", "bar");
     return myList;
'

lfun <- cxxfunction(signature(rList = "list"),
                    body     = code,
                    plugin   = "Rcpp")
-----------------------------------------------------------------------------

as can be seen below.

Do not underestimate the usefulness of 750+ regression tests in 330+
functions that are run with every release.  They do provide examples, even if
they are a little hard to read at first :)

Also, if an example does not compile, comment-out and simplify til it
does. The templated code is finicky. I often split very complex expression
into components as there is no shame in using temporary variables....

Dirk


R> library(inline)
R> 
R> code <- '
+      Rcpp::List myList(rList);
+      myList.push_back(10);
+      myList.push_back("foo", "bar");
+      return myList;
+ '
R> 
R> lfun <- cxxfunction(signature(rList = "list"),
+                     body     = code,
+                     plugin   = "Rcpp")
R> 
R>                   ## ,"list_push_back"=list(
R>                   ##  signature(x = "list"),
R>                   ##  'List list(x) ;
R>            ##   list.push_back( 10 ) ;
R>            ##   list.push_back( "bar", "foo" ) ;
R>            ##   return list ;
R>            ##  ')
R> lfun(list(a=1,b=-1))
$a
[1] 1

$b
[1] -1

[[3]]
[1] 10

$bar
[1] "foo"

R> 


-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list