[Rcpp-devel] Rcpp::List lost attributes after updating elements
Jiqiang Guo
guojq28 at gmail.com
Sun Dec 9 20:44:03 CET 2012
Well, those are good points. But I just do not know the size of my list a
priori, so I have to grow it along the way. It might not be optimal, but I
suppose as you said it is just suboptimal (i.e., not that bad).
Thanks,
Jiqiang
On Sun, Dec 9, 2012 at 2:25 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 9 December 2012 at 14:07, Jiqiang Guo wrote:
> | Note in the above, the printout for the second does not print any
> attribute as
> | for the first function.
>
> Hm. That it not desirable so maybe we can do better, but you should also
> note
> that you are using the list type in a suboptimal way by growing piece by
> piece which leads to __copies__ being made at which point we may cast to
> vector and drop the attributes.
>
> Here is a ever-so-slightlt modified example that works:
>
>
> -----------------------------------------------------------------------------
> #include <Rcpp.h>
>
> // [[Rcpp::export]]
> SEXP crtlist3() {
> Rcpp::List lst(2); // reserve known size
> lst[0] = 10;
> lst.attr("att1") = 1;
> lst[1] = 20;
> return lst;
> }
>
> -----------------------------------------------------------------------------
>
> R> sourceCpp("/tmp/jiqiang.cpp")
> R> crtlist3()
> [[1]]
> [1] 10
>
> [[2]]
> [1] 20
>
> attr(,"att1")
> [1] 1
> R>
>
>
> Essentially I do two things differently: reserve the needed size, and
> index
> by position. The attr then remains.
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121209/a77ceaf7/attachment.html>
More information about the Rcpp-devel
mailing list