繁体   English   中英

Rcpp将长度为0(NULL)的向量传递给cppfunction

[英]Rcpp pass vector of length 0 (NULL) to cppfunction

我有一个带向量的ints作为输入的cppFunction ,例如:

library(Rcpp)
cppFunction('double test2(NumericVector ints) {
            return 42;
            }')

如果传递长度至少为1的向量,则输出正确:

> test2(1)
[1] 42
> test2(1:10)
[1] 42

对于长度为0的输入,我得到:

> test2(c())
Error: not compatible with requested type

有没有办法将长度为0或更大的向量传递给我的函数? 即我的预期输出是:

> test2_expectedoutput(c())
[1] 42

我知道我可以通过首先检入R并调用函数的不同版本来控制R,但是我想避免这种情况。 我希望有一些简单的解决方案,因为在cpp内我也可以有一个长度为0的NumericVector ,如果我正确理解NumericVector zero; 确实。 我能找到的唯一相关问题就是如何将一个NULL对象从Rcpp函数返回到R。

几个月前,我们添加了传递为Nullable<T>的能力,这可能是你想要的。

这是一个简单的例子:

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
bool checkNull(Nullable<NumericVector> x) {
  if (x.isNotNull()) {
    // do something
    NumericVector xx(x);
    Rcpp::Rcout << "Sum is " << sum(xx) << std::endl;
    return true;
  } else {
    // do nothing
    Rcpp::Rcout << "Nothing to see" << std::endl;
    return false;
  }
}

/*** R
checkNull(1:3)
checkNull(NULL)
*/

及其输出:

R> sourceCpp("/tmp/null.cpp")

R> checkNull(1:3)
Sum is 6
[1] TRUE

R> checkNull(NULL)
Nothing to see
[1] FALSE
R> 

通过模仿,我们尊重预期的类型,但明确区分存在与否。

c()调用产生NULL ,它不是numeric向量。 这会在调用test2时生成错误。 您可以构建一个长度为0到numeric的数字向量:

#check what `c()` does
str(c())
# NULL

# now we try numeric(0)
test2(numeric(0))
#[1] 42

作为一个建议,我认为很少直接调用CFortranC++函数; 编写一个执行一些初步操作的包装器会更好,例如类型转换和类似操作。 类似于以下内容:

test2Wrapp<-function(x) test2(as.numeric(x))
test2Wrapp(c())
#[1] 42
#This has the benefit to not calling the internal routines in cases where conversion isn't possible
test2Wrapp(iris)
#Error: (list) object cannot be coerced to type 'double'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM