[Rcpp-devel] Returning an element from a List with its attributes?

Kevin Ushey kevinushey at gmail.com
Tue Aug 20 23:40:34 CEST 2013


Hi Tal,

In your example, note that x[[1]] is not a list; hence it must first be
coerced to a list, and attributes are lost in this conversion. This is
fairly standard in R code; eg x <- 1; attr(x, "animal") <- "cat"; x;
as.list(x), so you can't blame Rcpp for being consistent with R here.

The onus is on you to be careful with your types when setting attributes.
You should assume they are lost if you do any conversion. Indeed, the
non-standard attributes are very brittle.

That said, if you want the size of an RObject, there is always the C API to
fall back on: Rf_length(x) gives you the length of the object, agnostic to
its type.

-Kevin


On Tue, Aug 20, 2013 at 2:31 PM, Tal Galili <tal.galili at gmail.com> wrote:

> Dear Wush and others,
> Just to followup and say I've used this idea here:
> https://github.com/talgalili/dendextendRcpp/blob/master/src/cut_lower.cpp
>
> In general, it would have been better for me, where it possible to do some
> (or all) of the things below:
> 1) Get the length of a List
> 2) Change the attribute of elements within a list
> 3) Have this not happen:
>
> require(Rcpp)
> cppFunction('
>             SEXP temp(List x){
>             return(x);
>             }
>             ')
> x = list(structure(1L, animal = "cat"))
> temp(x)
> temp(x[[1]]) # I wish this didn't happen
>
> (I'm possibly missing out things, but I thought of listing it just in case)
>
>
> Many thanks for the help!
> Tal
>
>
>
>
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
>
> ----------------------------------------------------------------------------------------------
>
>
>
> On Tue, Aug 20, 2013 at 12:25 PM, Tal Galili <tal.galili at gmail.com> wrote:
>
>> Hello Wush,
>> Indeed, thank you!
>>
>> I will write more on this after I finish the code, since there are some
>> bits which are a mystery to me.
>>
>> Tal
>>
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili at gmail.com |
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>>
>> ----------------------------------------------------------------------------------------------
>>
>>
>>
>> On Tue, Aug 20, 2013 at 12:13 PM, Wush Wu <wush978 at gmail.com> wrote:
>>
>>> Hi Tal,
>>>
>>> ```
>>> cppFunction('
>>> std::vector<RObject>  temp2_fail(RObject x){
>>>    std::vector<RObject> lower;
>>>    List x_list(wrap(x));
>>>    int n = x_list.size();
>>>    lower.push_back(x);
>>>    return(lower) ;
>>> }
>>>             ')
>>> ```
>>>
>>> Is this what you want?
>>>
>>>
>>> 2013/8/20 Tal Galili <tal.galili at gmail.com>
>>>
>>>> Dear list members,
>>>>
>>>> I would like to be able to get an element pushed with its attributes,
>>>> and I came across the following problem:
>>>>
>>>> cppFunction('
>>>> std::vector<List>  temp(List x){
>>>>    std::vector<List> lower;
>>>>    lower.push_back(x);
>>>>    return(lower) ;
>>>> }
>>>>             ')
>>>> x = list(structure(1L, animal = "cat"))
>>>> temp(x)
>>>> temp(x[1])
>>>> temp(x[[1]]) # is there a way to not make this output without
>>>> attributes?
>>>>
>>>>
>>>>
>>>> If I use RObject, it will solve it, but then I would later not be able
>>>> to iterate over the object, because I can't seem to get the .size() of an
>>>> RObject.
>>>>
>>>> Here is the code with the RObject:
>>>>
>>>>
>>>> cppFunction('
>>>> std::vector<RObject>  temp2(RObject x){
>>>>    std::vector<RObject> lower;
>>>>    lower.push_back(x);
>>>>    return(lower) ;
>>>> }
>>>>             ')
>>>> x = list(structure(1L, animal = "cat"))
>>>> temp2(x)
>>>> temp2(x[1])
>>>> temp2(x[[1]]) # Now it works as I want, but the following wouldn't work:
>>>>
>>>>
>>>>
>>>>
>>>> cppFunction('
>>>> std::vector<RObject>  temp2_fail(RObject x){
>>>>    std::vector<RObject> lower;
>>>>    int n x.size();
>>>>    lower.push_back(x);
>>>>    return(lower) ;
>>>> }
>>>>             ')
>>>>
>>>>
>>>>
>>>> Any ideas?
>>>>
>>>>
>>>>
>>>>
>>>> ----------------Contact
>>>> Details:-------------------------------------------------------
>>>> Contact me: Tal.Galili at gmail.com |
>>>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew)
>>>> | www.r-statistics.com (English)
>>>>
>>>> ----------------------------------------------------------------------------------------------
>>>>
>>>>
>>>> _______________________________________________
>>>> Rcpp-devel mailing list
>>>> Rcpp-devel at lists.r-forge.r-project.org
>>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>>>
>>>
>>>
>>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130820/f1155830/attachment-0001.html>


More information about the Rcpp-devel mailing list