0

I'm trying to write a function which can take in functions as its arguments in Rcpp. I have written an example function in R that shows the kind of functionality that I'm aiming for:

simulate_and_evaluate <- function(simulate, evaluate) {
  y <- simulate(1)
  eval <- evaluate(y)
  return(eval)
}

simulate_fun <- function(n) rnorm(n, 0, 1)
evaluate_fun <- function(x) dnorm(x, 0, 1)

simulate_and_evaluate(simulate = simulate_fun,
                      evaluate = evaluate_fun)

In this function simulate_and_evaluate, this takes in two arguments which are both functions, one that simulates a number and one that evaluates a function with this simualted number. So as an example, we can simulate a value from a standard normal and evaluate the density of a standard normal at that point. Does anyone know if there's a way to do this in Rcpp?

4
  • 3
    Is there a reason you are tying to use Rcpp here? Is it to speed up code? Because if you are trying to call a standard R function in c++ land you probably wont get much of a boost. Rcpp works create if you are running efficient c++ code. But that doesn't seem to be what you are trying to do here. Commented Dec 12, 2019 at 21:36
  • 1
    There's a guide on doing this here Commented Dec 12, 2019 at 21:38
  • 2
    Also see package RcppDE with a worked example of passing compiled user-supplied functions around to an optimizer. Commented Dec 13, 2019 at 1:34
  • @MrFlick In the actual functions that I wish to implement, I take in these functions to simulate and evaluate densities, but also perform other operations with these results. I hoped that by being able to turn some of the R code that did this into Rcpp code, this would give me some improved performance. I understand that by just calling the basic R functions in C++ wouldn't improve performance, but hoped the other things the function would do with the results of these functions would increase efficiency. Commented Dec 13, 2019 at 13:07

1 Answer 1

6

Rcpp aims for seamless interfacing of R and C++ objects. As functions are first class R objects represented internally as a type a SEXP can take, we can of course also ship them with Rcpp. There are numerous examples.

So here we simply rewrite your function as a C++ function:

Rcpp::cppFunction("double simAndEval(Function sim, Function eval) {
  double y = as<double>(sim(1));
  double ev = as<double>(eval(y));
  return(ev);
}")

And we can then set the RNG to the same value, run your R function and this C++ function and get the same value. Which is awesome.

R> set.seed(123)
R> simulate_and_evaluate(simulate = simulate_fun,
+ evaluate = evaluate_fun)
[1] 0.341
R> set.seed(123) # reset RNG
R> simAndEval(simulate_fun, evaluate_fun)
[1] 0.341
R> 

But as @MrFlick warned you, this will not run any faster because we added no compiled execution of the actual functions we are merely calling them from C++ rathern than R.

The topic has been discussed before. Please search StackOverflow, maybe with a string [rcpp] Function to get some meaningful hits.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.