[Rcpp-devel] moving lists between functions in a package

Hadley Wickham h.wickham at gmail.com
Fri Dec 27 23:10:11 CET 2013


BTW since you have `using namespace Rcpp;` you don't need `Rcpp::` in
front of every function from the Rcpp namespace

Hadley

On Fri, Dec 27, 2013 at 3:21 PM, Scott Monroe <monroeleescott at gmail.com> wrote:
> Thanks so much.  Sorry for the silly error in the example.  The following
> works, and has the basic functionality I'm looking for.
>
> #include <RcppArmadillo.h>
> using namespace Rcpp ;
> // [[Rcpp::depends(RcppArmadillo)]]
>
> // [[Rcpp::export]]
> List check_B(arma::mat B) {
>   int p = B.n_cols;
>   arma::mat BB = B*B;
>   arma::vec X = B.col(0);
>
>   return Rcpp::List::create(Rcpp::Named("X")=X, Rcpp::Named("BB")=BB,
> Rcpp::Named("p")=p);
> }
>
> // [[Rcpp::export]]
> List check_A(NumericMatrix A) {
>   arma::mat B = Rcpp::as<arma::mat>(A);
>   Rcpp::List L = check_B(B);
>   arma::mat CC = L["BB"];
>
>   return Rcpp::List::create(Rcpp::Named("B")=B, Rcpp::Named("CC")=CC);
> }
>
>
> On Fri, Dec 27, 2013 at 12:51 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>>
>>
>> On 27 December 2013 at 12:36, Scott Monroe wrote:
>> | I am trying to figure out how to create a package where I have one
>> function
>> | call another, which returns a list.  Some example .cpp code is below.
>> If I
>> | just compile the first function, everything is fine.  When I add the
>> second
>> | function, I get errors.  I'm sure I'm using the Rcpp::List syntax
>> incorrectly,
>> | but I haven't had luck finding an example of how to do this.
>> |
>> | Again, I'd like to be able to call the first function from the second,
>> grab the
>> | results from the returned list, and do further computations.
>> |
>> | Thanks, Scott
>> |
>> |
>> | #include <RcppArmadillo.h>
>> | using namespace Rcpp ;
>> | // [[Rcpp::depends(RcppArmadillo)]]
>> |
>> | // [[Rcpp::export]]
>> | List check_C(NumericMatrix B) {
>> |   arma::mat A = Rcpp::as<arma::mat>(B);
>> |   int p = A.n_cols;
>> |   arma::mat AA = A*A;
>> |   arma::vec X = A.col(0);
>> |
>> |   return Rcpp::List::create(Rcpp::Named("X")=X, Rcpp::Named("AA")=A,
>> | Rcpp::Named("p")=p);
>> | }
>> |
>> | // [[Rcpp::export]]
>> | List check_C(NumericMatrix A) {
>> |   Rcpp::List L = check_C(A);
>>
>> A comes in as a matrix, now you are trying to make it a List -- that
>> won't.
>>
>> Plus, as Simon, observed you may want to rename the functions a little.
>>
>> Dirk
>>
>> |
>> |   return Rcpp::List::create(Rcpp::Named("A")=A);
>> | }
>> |
>> |
>> |
>> |
>> | ----------------------------------------------------------------------
>> | _______________________________________________
>> | 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
>> --
>> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
>
>
> _______________________________________________
> 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://had.co.nz/


More information about the Rcpp-devel mailing list