Как показывают существующие примеры, вы должны инстанцировать объект, когда находитесь в случае 'not NULL
':
R> Rcpp::cppFunction('int getdim(Nullable<NumericMatrix> X_mat) {
+ if(X_mat.isNotNull()) {
+ NumericMatrix M(X_mat);
+ int col_num = M.ncol();
+ return col_num;
+ }else{
+ return 0;
+ }
+ }')
R> getdim(NULL)
[1] 0
R> getdim(matrix(1:4,2))
[1] 2
R>