Skip to content

[Rcpp-devel] redimension help

2 messages · Christian Gunning, Silkworth,David J.

#
If this is your goal, I recommend an alternate method --
RcppArmadillo.  It offers resizing (e.g.
http://arma.sourceforge.net/docs.html#set_size
).  There are several ways to do this with Armadillo, depending on
whether the memory of the object gets preserved or not. Note that you
need to wrap() arma objects before, for example, putting them in lists
(i think) or returning them to R.  Something like this should get you
started:

require(RcppArmadillo)

src = '
mymat = arma::mat(2, 2);
//find dims of mymat, put them in vars nrow and ncol
mymat.set_size(nrow, ncol);
// do things
return wrap(mymat);
'

fun = cxxfunction('', src, plugin='RcppArmadillo')

As an aside, are you *sure* that you can't allocate after computing
the size?  I'm with Douglas in the confused camp :)
After all, much of Rcpp's magic is via templates, which make objects a
lot more dynamic than you might expect (if you're come from, for
example, a fortran background).

-Christian
#
Thanks for all who got involved in my folly.
I have chosen the following path for now.

src <- '
int s = 7;  // result of original oversize estimate before process runs
int c=3;  //known column count established from a list argument (variable to function)
Rcpp::IntegerVector v(s);
Rcpp::IntegerMatrix m(s,c);

int r = 4;  // number of rows that more complex process found necessary to fill
for(int x=1; x<r+1;x++)  { v[x-1]=x; }  // just partial fill as process would
for(int j=0; j<r;j++)  { for(int i=0;i<r;i++)  {m(i,j)= (i+1)*(j+1);} }

Rcpp::DataFrame DF =
Rcpp::DataFrame::create(Rcpp::Named("v")=v,
 Rcpp::Named("m")=m);

Rcpp::IntegerVector my_r(1);
my_r[0]=r;

Rcpp::List L=Rcpp::List::create(DF,my_r);
return L;
'

 fun <- cxxfunction(signature(),src, plugin = "Rcpp")

fun_test<-fun()
RcppDF<-fun_test[[1]]
r<-fun_test[[2]]
RcppDF<-RcppDF[1:r,]


That is, just send it all back to R in a dataframe and let R do the clean-up.
I'm not certain how this will perform when r>120000.  This operation is only performed once though.  I also intend to develop a better estimate for s than, say 160000.