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

Ahmadou Dicko dicko.ahmadou at gmail.com
Fri Dec 27 22:11:11 CET 2013


No need to convert NumericMatrix to Armadillo matrix, Rcpp does that
automatically.
I also changed the name of your functions because they were the same.

#include <RcppArmadillo.h>
using namespace Rcpp ;
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
List list_check(arma::mat B) {
    int p = B.n_cols;
    arma::mat AA = B*B;
    arma::vec X = B.col(0);

    return List::create(_["X"] = X,
            _["AA"] = AA,
            _["p"] = p);

}

// [[Rcpp::export]]
List check_C(arma::mat A) {
    List L = list_check(A);
    return List::create(_["mylist"] = L); // recursive list
}



On Fri, Dec 27, 2013 at 8:36 PM, Scott Monroe <monroeleescott at gmail.com>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);
>
>   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
>



-- 
Ahmadou H. DICKO
statistician economist (Ingénieur Statisticien Économiste)
PhD candidate in Climate change economics
Faculty of economics and managment - Cheikh Anta Diop University
West African Science Service Center on Climate Change and Adaptated Land
Use (WASCAL)
Center for Development Research (ZEF) - University of Bonn
email : ahmadou.dicko at ucad.edu.sn
twitter : @dickoah
github : github/dickoa <https://github.com/dickoa>
tel : +221 33 827 55 16
portable: +221 77 123 81 69
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131227/9a7de36f/attachment.html>


More information about the Rcpp-devel mailing list