Skip to content

Additional Rcpp attributes #1182

Closed
Closed
@traversc

Description

@traversc

I'd like to see a "signature" attribute that allows you to customize the R function signature in RcppExports.R.

I modified src/attributes.cpp here to be able to do this.

The syntax is like this:

// [[Rcpp::export( signature = {verbose = getOption("verbose")} )]]
void test1(bool verbose) {
  Rcout << "The verbose paramter is set to: ";
  Rcout << verbose << std::endl;
}

And in RcppExports.R it looks like this:

test1 <- function(verbose = getOption("verbose")) {
    invisible(.Call(`_RcppTest_test1`, verbose))
}

I wrote a bunch of tests to make sure it doesn't break. Here's a more complicated example using sourceCpp:

library(Rcpp)
sourceCpp(code='
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export( rng = false, signature = {x = list("{A}", "B"), verbose = getOption("verbose")} )]]
int test_inline(List x, bool verbose) {
  Rcout << "The verbose paramter is set to: ";
  Rcout << verbose << std::endl;
  if(x.size() > 0) {
    CharacterVector first_element = x[0];
    Rcout << "The first element of list x is: ";
    Rcout << first_element << std::endl;
  }
  return x.size();
}')

test_inline()
# The verbose paramter is set to: 0
# The first element of list x is: "{A}"
# [1] 2
options(verbose=TRUE)
test_inline(list(1))
# The verbose paramter is set to: 1
# The first element of list x is: "1"
# [1] 1

The benefit of the "signature" attribute is that it lets you add arbitrary default parameters (lists, functions, environments, getOption, etc.). Yes you could write a wrapper function, but it would be nice to write it all inline.

Any interest in a pull request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions