It's my first post here! Woohoo!
I would like to have an Rcpp
function that would have an argument being a list with some specified default values of its entries. I would like this function to work similarly to the following R
function:
> nicetry_R = function(sv = list( a = rep(0, 2)) ) { sv$a }
> nicetry_R()
[1] 0 0
> nicetry_R(list(a=rep(1,2)))
[1] 1 1
In this function argument sv
is a list specified by default to contain vector a
set to rep(0,2)
.
What I tried in RcppArmadillo
is a solution that is working by refering to R_NilValue
in the argument List
specification. Here's the content of my nicetry.cpp
file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector nicetry (Nullable<List> sv = R_NilValue) {
NumericVector aux_a(2);
if (sv.isNotNull()){
List SV(sv);
aux_a = SV["a"];
}
return aux_a;
}
/*** R
nicetry()
nicetry(list(a=rep(1,2)))
*/
It does what I want it to do in the sense that it produces the desired outcome:
> nicetry()
[1] 0 0
> nicetry(list(a=rep(1,2)))
[1] 1 1
But I would prefer to have the default values of the list specified in the argument line. The example below is just one of my failed attempts:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector nicetry (
List sv = List::create(Named("a",NumericVector::create(0,0)))
) {
NumericVector aux_a = sv["a"];
return aux_a;
}
/*** R
nicetry()
*/
In this example, the argument declaration contains the list. It does not compile though. When I copy the declaration of the List from the argument declaration section leaving this section empty, and paste the line in the body of the function: List sv = List::create(Named("a",NumericVector::create(0,0)));
then the list is created and the outcome provided. But that's not what I want.
Do you think it's possible to declare such a list as an argument at all?
Thanks so much! Greetings, T