[Rcpp-devel] Rcpp sugar preserving attributes
Dirk Eddelbuettel
edd at debian.org
Sun Jan 4 16:32:12 CET 2015
Søren,
Happy New Year!
On 4 January 2015 at 09:53, Søren Højsgaard wrote:
| Dear all,
|
| I have noticed an issue with preserving attributes (or rather attributes that are not preserved). Consider this code where I take the log of an array:
Quite possible.
Attributes are just attached to objects, and we may well drop them along the
way. Which is a bug. Maybe you can start rewriting this in a way that
preserves attributes? log is one of many 'math' functions implemented via
macro expansion so if you get it right for you get it right for many...
Dirk
|
| #include <Rcpp.h>
| using namespace Rcpp;
|
| //[[Rcpp::export]]
| NumericVector fun1(NumericVector x){
| return log( x );
| }
|
| //[[Rcpp::export]]
| NumericVector fun2(NumericVector x){
| NumericVector out=clone(x), tmp=log(x);
| for (int i=0;i<x.size();++i)out[i] = tmp[i];
| return out;
| }
|
| /*** R
| x <- HairEyeColor; x
| fun1(x)
| fun2(x)
| */
|
|
| > fun1(x)
| [1] 3.4657359 3.9702919 2.3025851 1.0986123 2.3978953 3.9120230 2.3025851 3.4011974 2.3025851 3.2188758 1.9459101
| [12] 1.6094379 1.0986123 2.7080502 1.9459101 2.0794415 3.5835189 4.1896547 2.7725887 1.3862944 2.1972246 3.5263605
| [23] 1.9459101 4.1588831 1.6094379 3.3672958 1.9459101 1.6094379 0.6931472 2.6390573 1.9459101 2.0794415
|
| > fun2(x)
| , , Sex = Male
|
| Eye
| Hair Brown Blue Hazel Green
| Black 3.4657359 2.3978953 2.3025851 1.0986123
| Brown 3.9702919 3.9120230 3.2188758 2.7080502
| Red 2.3025851 2.3025851 1.9459101 1.9459101
| Blond 1.0986123 3.4011974 1.6094379 2.0794415
|
| , , Sex = Female
|
| Eye
| Hair Brown Blue Hazel Green
| Black 3.5835189 2.1972246 1.6094379 0.6931472
| Brown 4.1896547 3.5263605 3.3672958 2.6390573
| Red 2.7725887 1.9459101 1.9459101 1.9459101
| Blond 1.3862944 4.1588831 1.6094379 2.0794415
|
| fun2 is kludgy! Is there a more elegant way of preserving attributes using Rcpp sugar?
|
| All the best
| Søren
|
|
|
| _______________________________________________
| 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
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list