在 RcppArmadillo 类型中使用 Rcpp::Nullable 时出错 [英] Error using Rcpp::Nullable with RcppArmadillo types

查看:58
本文介绍了在 RcppArmadillo 类型中使用 Rcpp::Nullable 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 包中使用 RcppArmadillo,我想在参数列表中使用 Rcpp::Nullable.

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,
                Nullable<arma::mat> size_param = R_NilValue,
                const int& infinite = 100000, const bool& log_v = false)

这会产生如下错误:

Error: package or namespace load failed for ‘packagex’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so': dlopen(/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so, 6): 
Symbol not found: __Z5d_snbRN4Rcpp6VectorILi14ENS_15PreserveStorageEEENS_8NullableIS2_EES5_S5_RKiRKb
Referenced from: /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so
Expected in: flat namespace in /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so
Error: loading failed
Execution halted

目前唯一可能的解决方案似乎是将参数作为 NumericVector 获取,然后获取内容,然后将其转换为 Armadillo 类型.

The only possible solution right now seems to get the parameter as NumericVector and then get contents and then cast it into Armadillo types.

NumericVector d_snb(NumericVector& x,
                Nullable<NumericVector> size_param = R_NilValue ...)
{
...

  if(size_param.isNotNull()) {
    arma::mat test(NumericVector(size_param));
    param_check++;
  }
...
}

这是一个警告:

d_snb.cpp:36:21: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
  arma::mat test(NumericVector(size_param));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~
d_snb.cpp:36:22: note: add a pair of parentheses to declare a variable
  arma::mat test(NumericVector(size_param));
                 ^
                 (                        )
1 warning generated.

解决这个问题的最佳方法是什么?

What's the best way to go about this ?

推荐答案

是的,Rcpp::Nullable<> 仅适用于基于 SEXP 的 Rcpp 类型.

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

因此您必须执行您发现的两步程序.但我认为你可以做一个更简单的:arma::mat test = Rcpp::as<arma::mat>(size_param);

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;

}

还有一个快速演示:

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> 

这篇关于在 RcppArmadillo 类型中使用 Rcpp::Nullable 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆