4

I have a program written in C++ which calculates values for a likelihood function, which relies on lot of data. I want to be able to call the function from R to request function values (the calculations would take to much time in R, and the C++ program is already to long to change it, it's approximately 150K lines of code).

I can do this to request one value, but then the C++ application terminates and I have to restart it and load all the data again, (did this with .c()). The loading takes from 10-30 seconds, depending on the model for the likelihood function and the data, and I was thinking if there is a way to keep the C++ application alive, waiting for requests for function values, so I don't have to read all the data back into memory. Already calculating one function value in the C++ application takes around half a second, which is very long for C++.

I was thinking about using pipe() to do this, and ask you if that is a feasible option or should I use some other method? Is it possible to do this with rcpp?

I'm doing this to test minimizing algorithms for R on this function.

9
  • 6
    Maybe take a look at Rcpp ? cran.r-project.org/web/packages/Rcpp/index.html Commented Oct 10, 2013 at 11:12
  • Can't just system("R Stuff"); from c++? It's similar to this one : C call cmd command R example Commented Oct 10, 2013 at 11:17
  • pipes looks as probable solution, or maybe tcp server ? Commented Oct 10, 2013 at 11:21
  • Do you know of any examples where this is done with Rcpp? Or just any short examples of cimmunication between R and C++ processes? Found this, might help! Commented Oct 10, 2013 at 11:36
  • 2
    I find the question confused, and editing may help. Either you have a main C++ function in which you need some R functionality---which RInside is made for---or you have C++ code you want to call from R---which Rcpp makes easy. Either way, the Rcpp code is used to make data transfer easy. And in either case, it is the same process so pipe() et al are not appropriate. Unless you do something more complicated than you would want to. Commented Oct 10, 2013 at 12:06

2 Answers 2

7

Forget about .C. That is clunky. Perhaps using .C over .Call or .External made sense before Rcpp. But now with the work we've put in Rcpp, I really don't see the point of using .C anymore. Just use .Call.

Better still, with attributes (sourceCpp and compileAttributes), you don't even have to see the .Call anymore, it just feels like you are using a c++ function.

Now, if I wanted to do something that preserves states, I'd use a module. For example, your application is this Test class. It has methods do_something and do_something_else and it counts the number of times these methods are used:

#include <Rcpp.h>
using namespace Rcpp ;

class Test {
public:
    Test(): count(0){}

    void do_something(){
        // do whatever
        count++ ;
    }
    void do_something_else(){
        // do whatever
        count++ ;
    }

    int get_count(){
        return count ;
    }

private:
    int count ; 
} ;

This is pretty standard C++ so far. Now, to make this available to R, you create a module like this :

RCPP_MODULE(test){
    class_<Test>( "Test" )
        .constructor()
        .method( "do_something", &Test::do_something )
        .method( "do_something_else", &Test::do_something_else )

        .property( "count", &Test::get_count )
    ;
}

And then you can just use it :

app <- new( Test )
app$count
app$do_something()
app$do_something()
app$do_something_else()
app$count
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your response, I think I can definitely work this out by creating a module like this!
1

There are several questions here.

What is the best way to call C++ code from R?

As other commenters have pointed out, the Rcpp package provides the nicest interface. Using the .Call function from base R is also possible, but not recommended as nice as Rcpp.

How do I stop repeatedly passing data back and forth between R and C++?

You'll just just to restructure your code a little bit. Rewrite a wrapper routine in C++ that calls all the existing C++ routines, and call that from R.

4 Comments

The .Call() function is required as the .C() interface is limited, error-prone and should not be be used -- see eg Simon Urbanek on r-devel a few months back.
@DirkEddelbuettel Can you elaborate on why .C() is error-prone, or post a link to the r-devel message from Simon Urbanek?
@mrip: Here you go. R Core member Simon Urbanek on r-devel: "As usual, I can only say don't use .C()" I got the date wrong: this was already in 2012 -- time flies, but some people still think they should use .C(). Which is bad enough, but sadly it gets worse when they inflict such bad ideas on others.
Thanks. The reason I ask is that I have some C code that I call with .C(), and I haven't noticed any problems. I don't think it's all that bad -- it's a very simple C interface.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.