10

I have an R code with a bunch of user-defined R functions. I'm trying to make the code run faster and of course the best option is to use Rcpp. My code involves functions that call each other. Therefore, If I write some functions in C++, I should be able to call and to run some of my R functions in my c++ code. In a simple example consider the code below in R:

mySum <- function(x, y){
 return(2*x + 3*y)
}
x <<- 1
y <<- 1

Now consider the C++ code in which I'm trying to access the function above:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int mySuminC(){
 Environment myEnv = Environment::global_env();
 Function mySum = myEnv["mySum"];
 int x = myEnv["x"];
 int y = myEnv["y"];
 return wrap(mySum(Rcpp::Named("x", x), Rcpp::Named("y", y)));
 }

When I source the file in R with the inline function sourceCpp(), I get the error:

 "invalid conversion from 'SEXPREC*' to int

Could anyone help me on debugging the code? Is my code efficient? Can it be summarized? Is there any better idea to use mySum function than what I did in my code?

Thanks very much for your help.

3
  • 1
    possible duplicate of Calling an R function using inline and Rcpp is still just as slow as original R code Commented Jan 20, 2014 at 3:48
  • @DirkEddelbuettel, with a lot of respect, I don't agree - the link above addresses a totally different issue. Commented Jan 20, 2014 at 4:00
  • 1
    It states that a) it is easy to call a function, b) that it is no faster calling an R function from C++ than from R (and it wasn't clear whether you groked that) and c) your int conversion issue is an unrelated beginner's problem. Commented Jan 20, 2014 at 4:04

1 Answer 1

14

You declare that the function should return an int, but use wrap which indicates the object returned should be a SEXP. Moreover, calling an R function from Rcpp (through Function) also returns a SEXP.

You want something like:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
SEXP mySuminC(){
  Environment myEnv = Environment::global_env();
  Function mySum = myEnv["mySum"];
  int x = myEnv["x"];
  int y = myEnv["y"];
  return mySum(Rcpp::Named("x", x), Rcpp::Named("y", y));
}

(or, leave function return as int and use as<int> in place of wrap).

That said, this is kind of non-idiomatic Rcpp code. Remember that calling R functions from C++ is still going to be slow.

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

4 Comments

Yep. There are a few prior answers here making that same point.
@DirkEddelbuettel and Kevin, I have another question regarding this post. Consider a typical C++ program which has a bunch of functions declared outside of the int main() chunk and are used within the chunk. Now consider the body argument in cxxfunction(), to me body is like what is inside of the int main() chunk somewhat. My question is where to declare those functions that our outside of main() and are used inside of it in cxxfunction()?
@DirkEddelbuettel, absolutely.
how to get the return values from R function? Actually stackoverflow.com/a/21225890/9113303 here we calls the R function mySum. So we should get the return value return(2*x + 3*y). But i am getting Rcpp::sourceCpp('Desktop/cuda_pgm_cpp/mySum.cpp') as a result while running through R studio.

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.