1

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

Tomasz
  • 78
  • 5
  • 2
    Why are you using Armadillo? Why are you returning an Armadillo vector and not a `Rcpp::NumericVector`? (If I replace `vec` with `NumericVector` in your code, I get the expected result.) – Roland Aug 27 '21 at 10:38
  • Hey Roland, thanks for reading my post and pointing this out. I have rewritten my cpp function and added the presentation of the outcome after your comment. I think that this edit will make it clearer what my question is. The R function and the cpp one now give exactly the same and the demanded outcome. My question was about whether the cpp function could be rewritten so that the default values of the input List were defined in the argument declaration line entirely. Many thanks! Cheers, – Tomasz Aug 28 '21 at 11:58
  • 1
    Wrap the C++ function call in an R call with default parameters. This is what Rcpp does under the hood anyway. – thc Aug 31 '21 at 17:21
  • Hey @thc Many thanks for your reply! This seems to be the way to go and thanks for your indication of the direction. In fact, just the evening before you replied I had noticed in an article about Rcpp package preparation that folks do what you mentioned in that context, and I then I connected it to my question. I guess, I'll answer my own question by providing the codes for the solution over the weekend, once I clarify the details. – Tomasz Sep 02 '21 at 10:44

0 Answers0