[Rcpp-devel] How to set a default value when call a R function in Rcpp

Dirk Eddelbuettel edd at debian.org
Fri Nov 11 06:41:33 CET 2016


On 11 November 2016 at 05:16, Jingyu He wrote:
| I'm writing a R package by Rcpp and I have a problem when setting default value
| when call a R function in Rcpp. Here is an example to pass a R function to C++,
| provided by http://adv-r.had.co.nz/Rcpp.html
| 
| #include <Rcpp.h>
| using namespace Rcpp;
| // [[Rcpp::export]]
| RObject callWithOne(Function f) {
|   return f(1);
| }
| 
| The question is, how to set the default value for f? I would like to have the
| choice to pass a function or not, like
| 
| callWithOne(Function f = NULL) {
|   // if pass f, run f(1)
|   // else, run something else
| }


1) Don't crosspost. If you decided to post on StackOverflow, stay on
StackOverflow. Now that you posted here, maybe delete the question on
StackOverflow.

2) I don't understand the question.  Wouldn't the following do:


#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
RObject callWithOne(Function f) {
  const int arg = 1;
  return f(arg);
}


If it is a constant, as you seem to imply, why not make it one?

But if it is a variable, just pass it down.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list