Thanks for your advice, I now understand how to manipulate one-level lists:<br><br>fn &lt;- cxxfunction(signature(l_in=&quot;list&quot;),<br>                  body=&#39;<br>using namespace Rcpp;<br>List l(l_in);<br>IntegerVector lf = l[&quot;foo&quot;];<br>



CharacterVector lb = l[&quot;bar&quot;];<br>for(int i=0; i&lt;lf.size(); ++i)<br>  Rprintf(&quot;l[%s][%i] %i\\n&quot;, &quot;foo&quot;, i, lf[i]);<br>for(int i=0; i&lt;lb.size(); ++i)<br>  Rprintf(&quot;l[%s][%i] %s\\n&quot;, &quot;bar&quot;, i, std::string(lb[i]).c_str());<br>



&#39;, plugin=&quot;Rcpp&quot;, verbose=TRUE)<br>z &lt;- fn(list(foo=c(1,2,3,4),bar=c(&quot;bar1&quot;,&quot;bar2&quot;)))<br><br>But what about 2-level lists? Why the following code doesn&#39;t compile?<br><br>fn &lt;- cxxfunction(signature(l_in=&quot;list&quot;),<br>



                  body=&#39;<br>using namespace Rcpp;<br>List l(l_in);<br>List lf(l[&quot;foo&quot;]);<br>&#39;, plugin=&quot;Rcpp&quot;, verbose=TRUE)<br>z &lt;- fn(list(foo=list(bar=1)))<br><br>And what the following message mean? &quot;error: call of overloaded ‘Vector(Rcpp::internal::generic_name_proxy&lt;19&gt;)’ is ambiguous&quot;<br>


<br>I had a look at &quot;runit.Vector.R&quot;
on r-forge, but couldn&#39;t find any test involving 2-level (or more) lists, although on SO in June 2010 (<a href="http://stackoverflow.com/questions/3088650/how-do-i-create-a-list-of-vectors-in-rcpp/3088744#3088744" target="_blank">http://stackoverflow.com/questions/3088650/how-do-i-create-a-list-of-vectors-in-rcpp/3088744#3088744</a>), you said that it should work.<br>


<br>I checked that I can create a 2-level list, but the code below doesn&#39;t compile if I uncomment the last Rprintf line:<br><br>fn &lt;- cxxfunction(signature(),<br>                  body=&#39;<br>using namespace Rcpp;<br>


IntegerVector vi(2);<br>vi[0] = 2;<br>vi[1] = 8;<br>List ll = List::create(Named(&quot;bar&quot;)=vi);<br>Rprintf(&quot;ll.size %i\\n&quot;, ll.size());<br>List l = List::create(Named(&quot;foo&quot;)=ll);<br>Rprintf(&quot;l.size %i\\n&quot;, l.size());<br>


//Rprintf(&quot;l.ll.size %i\\n&quot;, l[&quot;foo&quot;].size());<br>return l;<br>&#39;, plugin=&quot;Rcpp&quot;, verbose=TRUE)<br>print(fn())<br><br>Thus once again I&#39;m stuck, but if I know how to access 2-level lists, I think I will be able to go back to my original problem, and stop sending emails on this mailing list ;)<br>

<br><div class="gmail_quote">On Fri, Aug 12, 2011 at 8:09 AM, Dirk Eddelbuettel <span dir="ltr">&lt;<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><br>
On 12 August 2011 at 01:22, Walrus Foolhill wrote:<br>
| Ok, I started with smaller examples. I understand more or less how to<br>
| manipulate IntegerVectors, but not StringVectors (see below), and thus I can&#39;t<br>
| even start manipulating a simple list of StringVectors. Even so I looked at<br>
| mailing lists, StackOverflow, package pdf, source code on R-Forge...<br>
|<br>
| The following code tells me &quot;warning: cannot pass objects of non-POD type<br>
| ‘struct Rcpp::internal::string_proxy&lt;16&gt;’ through ‘...’; call will abort at<br>
| runtime&quot;: why does it complain about printing the string in vec_s[i]?<br>
<br>
</div>Again, simpler helps. That is the standard C / C++ error message of<br>
<br>
        std:string foo = &quot;bar&quot;;<br>
        printf(&quot;String is %s \n&quot;, foo);<br>
<br>
where you need foo.c_str() to pass a char* to printf.<br>
<div><br>
| fn &lt;- cxxfunction(signature(l_in=&quot;list&quot;),<br>
|                   body=&#39;<br>
| using namespace Rcpp;<br>
| List l = List(l_in);<br>
| Rprintf(&quot;list size: %d\\n&quot;, l.size());<br>
|<br>
| IntegerVector vec_i= IntegerVector(2);<br>
| vec_i[0] = 1;<br>
| vec_i[1] = 2;<br>
| List l2 = List::create(_[&quot;vec&quot;] = vec_i);<br>
| Rprintf(&quot;vec_i size: %d\\n&quot;, vec_i.size());<br>
| for(int i=0; i&lt;vec_i.size(); ++i)<br>
|   Rprintf(&quot;vec_i[%d]=%d\\n&quot;, i, vec_i[i]);<br>
|<br>
| StringVector vec_s = StringVector::create(&quot;toto&quot;);<br>
| vec_s[0] = &quot;toto&quot;;<br>
| Rprintf(&quot;vec_s size: %d\\n&quot;, vec_s.size());<br>
| for(int i=0; i&lt;vec_s.size(); ++i)<br>
|   Rprintf(&quot;vec_s[%d]=%s\\n&quot;, i, vec_s[i]);<br>
<br>
</div>Try vec_s[i].c_str() instead.<br>
<font color="#888888"><br>
Dirk<br>
</font><div><div></div><div><br>
| return l2;<br>
| &#39;,<br>
|                   plugin=&quot;Rcpp&quot;, verbose=TRUE)<br>
| print(fn(list(a=c(1,2,3), b=c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;))))<br>
|<br>
| Moreover, how can I access the component of a list given as input, as &quot;l_in&quot;<br>
| above? Should I use l.begin()? or l[1]? or l[&quot;a&quot;]? none of them seems to<br>
| compile successfully.<br>
|<br>
| On Thu, Aug 11, 2011 at 8:54 PM, Dirk Eddelbuettel &lt;<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>&gt; wrote:<br>
|<br>
|<br>
|     Howdy,<br>
|<br>
|     On 11 August 2011 at 20:44, Walrus Foolhill wrote:<br>
|     | Ok, thanks for your answer, but I wasn&#39;t clear enough. So here are more<br>
|     details<br>
|     | of what I want to do.<br>
|     |<br>
|     | I have one list named &quot;probes&quot;:<br>
|     | probes &lt;- list(chr1=data.frame(name=c(&quot;p1&quot;,&quot;p2&quot;),<br>
|     |                  start=c(81,95),<br>
|     |                  end=c(85,100),<br>
|     |                  stringsAsFactors=FALSE))<br>
|     |<br>
|     | I also have one list named &quot;genes&quot;:<br>
|     | genes &lt;- list(chr1=data.frame(name=c(&quot;g1&quot;,&quot;g2&quot;), start=c(11,111), end=c<br>
|     | (90,190)),<br>
|     |                 chr2=data.frame(name=&quot;g3&quot;, start=11, end=90))<br>
|     |<br>
|     | I need to compare those two lists in order to obtain the following list<br>
|     which<br>
|     | contains, for each gene, the name of the probes included in it:<br>
|     | links &lt;- list(chr1=list(g1=c(&quot;p1&quot;)))<br>
|     |<br>
|     | Here is my R function (assuming that the probes are sorted based on their<br>
|     start<br>
|     | and end coordinates):<br>
|     |<br>
|     | fun.l &lt;- function(genes, probes){<br>
|     |   links &lt;- lapply(names(genes), function(<a href="http://chr.name" target="_blank">chr.name</a>){<br>
|     |     if(! <a href="http://chr.name" target="_blank">chr.name</a> %in% names(probes))<br>
|     |       return(NULL)<br>
|     |    <br>
|     |     res &lt;- list()<br>
|     |    <br>
|     |     genes.c &lt;- genes[[<a href="http://chr.name" target="_blank">chr.name</a>]]<br>
|     |     probes.c &lt;- probes[[<a href="http://chr.name" target="_blank">chr.name</a>]]<br>
|     |    <br>
|     |     for(<a href="http://gene.name" target="_blank">gene.name</a> in genes.c$name){<br>
|     |       gene &lt;- genes.c[genes.c$name == <a href="http://gene.name" target="_blank">gene.name</a>,]<br>
|     |       res[[<a href="http://gene.name" target="_blank">gene.name</a>]] &lt;- vector()<br>
|     |       for(<a href="http://probe.name" target="_blank">probe.name</a> in probes.c$name){<br>
|     |         probe &lt;- probes.c[probes.c$name == <a href="http://probe.name" target="_blank">probe.name</a>,]<br>
|     |         if(probe$start &gt;= gene$start &amp;&amp; probe$end &lt;= gene$end)<br>
|     |           res[[<a href="http://gene.name" target="_blank">gene.name</a>]] &lt;- append(res[[<a href="http://gene.name" target="_blank">gene.name</a>]], <a href="http://probe.name" target="_blank">probe.name</a>)<br>




|     |         else if(probe$start &gt; gene$end)<br>
|     |           break<br>
|     |       }<br>
|     |       if(length(res[[<a href="http://gene.name" target="_blank">gene.name</a>]]) == 0)<br>
|     |         res[[<a href="http://gene.name" target="_blank">gene.name</a>]] &lt;- NULL<br>
|     |     }<br>
|     |    <br>
|     |     if(length(res) == 0)<br>
|     |       res &lt;- NA<br>
|     |     return(res)<br>
|     |   })<br>
|     |   names(links) &lt;- names(genes)<br>
|     |   links &lt;- Filter(function(links.c){!is.null(links.c)}, links)<br>
|     |   return(links)<br>
|     | }<br>
|     |<br>
|     | And here is the beginning of my attempt using Rcpp:<br>
|     |<br>
|     | src &lt;- &#39;<br>
|     | using namespace Rcpp;<br>
|     |<br>
|     | List genes = List(genes_in);<br>
|     | int genes_nb_chr = genes.length();<br>
|     | std::vector&lt;std::string&gt; genes_chr = genes.names();<br>
|     |<br>
|     | List probes = List(probes_in);<br>
|     | int probes_nb_chr = probes.length();<br>
|     |<br>
|     | std::vector&lt; std::vector&lt;std::string&gt; &gt; links;<br>
|     |<br>
|     | // the main task is performed in this loop<br>
|     | for(int chrnum=0; chrnum&lt;genes_nb_chr; ++chrnum){<br>
|     |   DataFrame genes_c = DataFrame(genes[chrnum]);<br>
|     |   // ... add code to map probes on genes, that is fill &quot;links&quot; ...<br>
|     | }<br>
|     |<br>
|     | return wrap(links);<br>
|     | &#39;<br>
|     |<br>
|     | funC &lt;- cxxfunction(signature(genes_in=&quot;list&quot;,<br>
|     |                                 probes_in=&quot;list&quot;),<br>
|     |                       body=src, plugin=&quot;Rcpp&quot;)<br>
|     |<br>
|     | The problem starts quite early: when I compile this piece of code, I get<br>
|     | &quot;error: call of overloaded ‘DataFrame(Rcpp::internal::generic_proxy&lt;19&gt;)’<br>
|     is<br>
|     | ambiguous&quot;.<br>
|<br>
|     Try a simpler mock-up. I don&#39;t have it in me to work through this now.<br>
|     DataFrames are a little different from C++ -- start by trying to summarize<br>
|     in<br>
|     just a vector, or collection of vectors.<br>
|<br>
|     | What should I do to go through the &quot;probes&quot; and &quot;genes&quot; lists given as<br>
|     input?<br>
|     | Maybe more generically, how can we go through a list of lists (of<br>
|     lists...)<br>
|     | with Rcpp?<br>
|     |<br>
|     | 2nd (small) question, I don&#39;t manage to use Rprintf when using inline,<br>
|     for<br>
|     | instance Rprintf(&quot;%d\n&quot;, i);, it complains about the quotes. What should<br>
|     I do<br>
|     | to print statement from within the for loop?<br>
|<br>
|     The backslashes need escaping as in<br>
|<br>
|      R&gt; printing &lt;- cxxfunction(, plugin=&quot;Rcpp&quot;, body=&#39; Rprintf(&quot;foo\\n&quot;); &#39;)<br>
|      R&gt; printing()<br>
|      foo<br>
|      NULL<br>
|      R&gt;<br>
|<br>
|     | Thanks in advance. As my question is very long, I won&#39;t mind if you tell<br>
|     me to<br>
|     | find another way by myself. But maybe one of you can put me on the good<br>
|     track.<br>
|<br>
|     You are doing good but you have decent size problem. Try breaking into<br>
|     smaller pieces and a handle on each problem in turn.<br>
|<br>
|     Dirk<br>
|<br>
|     |<br>
|     | On Thu, Aug 11, 2011 at 7:00 AM, Dirk Eddelbuettel &lt;<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>&gt;<br>
|     wrote:<br>
|     |<br>
|     |<br>
|     |     On 11 August 2011 at 03:06, Walrus Foolhill wrote:<br>
|     |     | Hello,<br>
|     |     | I need to create a list and then fill it sequentially by adding<br>
|     |     components in a<br>
|     |     | for loop. Here is an example that works:<br>
|     |     |<br>
|     |     | library(inline)<br>
|     |     | src &lt;- &#39;<br>
|     |     | Rcpp::List mylist(2);<br>
|     |     | for(int i=0; i&lt;2; ++i)<br>
|     |     |   mylist[i] = i;<br>
|     |     | mylist.names() = CharacterVector::create(&quot;a&quot;,&quot;b&quot;);<br>
|     |     | return mylist;<br>
|     |     | &#39;<br>
|     |     | fun &lt;- cxxfunction(body=src, plugin=&quot;Rcpp&quot;)<br>
|     |     | print(fun())<br>
|     |     |<br>
|     |     | But what I really want is to create an empty list and then fill it,<br>
|     that<br>
|     |     is<br>
|     |     | without specifying its number of components before hand... This is<br>
|     |     because I<br>
|     |     | don&#39;t know in advance at which step of the for loop I will need to<br>
|     create<br>
|     |     a new<br>
|     |     | component. Here is an example, that obviously doesn&#39;t work, but<br>
|     that<br>
|     |     should<br>
|     |     | show what I am looking for:<br>
|     |     |<br>
|     |     | Rcpp::List mylist;<br>
|     |     | CharacterVector names = CharacterVector::create(&quot;a&quot;, &quot;b&quot;);<br>
|     |<br>
|     |     If you know how long names is, you know how long mylist going to be<br>
|     ....<br>
|     |<br>
|     |     | for(int i=0; i&lt;2; ++i){<br>
|     |     |   mylist.add(names[i], IntegerVector::create());<br>
|     |     |   mylist[names[i]].push_back(i);<br>
|     |<br>
|     |     I don&#39;t understand what that is trying to do.<br>
|     |<br>
|     |     | }<br>
|     |     | return mylist;<br>
|     |     |<br>
|     |     | Do you know how I could achieve this? Thanks.<br>
|     |<br>
|     |     Rcpp::List is an alias for Rcpp::GenericVector, and derives from<br>
|     Vector.<br>
|     |     You<br>
|     |     can look at the public member functions -- there are things like<br>
|     |<br>
|     |        push_back()<br>
|     |        push_front()<br>
|     |        insert()<br>
|     |<br>
|     |     etc that behave like STL functions __but are inefficient as we<br>
|     (almost<br>
|     |     always) need to copy the whole object__ so they are not recommended.<br>
|     |<br>
|     |     When I had to deal with &#39;unknown quantities of data&#39; returning I was<br>
|     mostly<br>
|     |     able to either turn it into a &#39;fixed or known columns, unknow rows&#39;<br>
|     problem<br>
|     |     (easy, just grow row-wise) or I &#39;cached&#39; in a C++ data structure<br>
|     first<br>
|     |     before<br>
|     |     returning to R via Rcpp structures -- and then I knew the dimensions<br>
|     for<br>
|     |     the<br>
|     |     to-be-created object too.<br>
|     |<br>
|     |     Dirk<br>
|     |<br>
|     |<br>
|     |     --<br>
|     |     Two new Rcpp master classes for R and C++ integration scheduled for<br>
|     |     New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
|     |     <a href="http://dirk.eddelbuettel.com/blog/2011/08/04#" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#</a><br>
|     |     rcpp_classes_2011-09_and_2011-10<br>
|     |<br>
|     |<br>
|<br>
|     --<br>
|     Two new Rcpp master classes for R and C++ integration scheduled for<br>
|     New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
|     <a href="http://dirk.eddelbuettel.com/blog/2011/08/04#" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#</a><br>
|     rcpp_classes_2011-09_and_2011-10<br>
|     <a href="http://www.revolutionanalytics.com/products/training/public/" target="_blank">http://www.revolutionanalytics.com/products/training/public/</a><br>
|     rcpp-master-class.php<br>
|<br>
|<br>
<br>
</div></div>--<br>
<div><div></div><div>Two new Rcpp master classes for R and C++ integration scheduled for<br>
New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
<a href="http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10</a><br>
<a href="http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php" target="_blank">http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php</a><br>
</div></div></blockquote></div><br>