简体   繁体   中英

Rcpp pass vector of length 0 (NULL) to cppfunction

I have a cppFunction with a vector ints as input, eg:

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

The output is correct if passing a vector of length at least 1:

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

For input of length 0 however I get:

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

Is there any way to pass a vector of length 0 or larger to my function? Ie my expected output is:

> test2_expectedoutput(c())
[1] 42

I know I could control for this in R by checking in R first and calling a different version of the function but would like to avoid this. I expect there is some easy solution out there since within cpp I could also have a NumericVector of length 0 if I understand correctly what NumericVector zero; does. The only related question I could find was this on how to return a NULL object from within a Rcpp function to R .

A few months ago we added the ability to pass as Nullable<T> which may be what you want here.

Here is a simple example:

#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)
*/

and its output:

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

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

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

By being templated we respect the intended type but clearly differentiate between being there, and not.

The c() calls produces NULL which is not a numeric vector. This generates the error when test2 is called. You can build a numeric vector of length 0 through numeric :

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

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

As a suggestion, I think that C , Fortran or C++ functions should rarely be called directly; much better to write a wrapper that does some preliminary operations, like type conversions and similar. Something like the following:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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