Error using Rcpp::Nullable with RcppArmadillo types

前端 未结 1 1344
走了就别回头了
走了就别回头了 2020-12-20 06:25

I\'m using RcppArmadillo in my R package and I would like to use the Rcpp::Nullable in the parameter list.

NumericVector d_snb(NumericVector& x,
                 


        
相关标签:
1条回答
  • 2020-12-20 07:05

    Yes, Rcpp::Nullable<> works only around SEXP-based Rcpp types.

    So you have to do the two-step procedure you found. But I think you can do one simpler: arma::mat test = Rcpp::as<arma::mat>(size_param);

    Here is a complete example which compiles (but does 'nothing'):

    #include <RcppArmadillo.h>
    // [[Rcpp::depends(RcppArmadillo)]]
    
    // [[Rcpp::export]]
    void d_snb(Rcpp::NumericVector& x,
               Rcpp::Nullable<Rcpp::NumericVector> size_param = R_NilValue) {
    
      if (size_param.isNotNull()) {
        arma::vec test = Rcpp::as<arma::vec>(size_param);
        test.print("vector");
      }
      Rcpp::Rcout << x << std::endl;
    
    }
    

    And a quick demo:

    R> sourceCpp("/tmp/so44102346.cpp")
    R> d_snb(c(1,2,3))
    1 2 3
    R> d_snb(c(1,2,3), c(4,5,6))
    vector
       4.0000
       5.0000
       6.0000
    1 2 3
    R> 
    
    0 讨论(0)
提交回复
热议问题