The relationship between R and C ++

I have a program written in C ++ that calculates the values ​​of a likelihood function that relies on a ton of data. I want to be able to call a function from R to request function values ​​(calculations would require a lot of time in R, and a C ++ program should have changed it for a long time, it's about 150K lines of code).

I can do this to request a single value, but then the C ++ application terminates, and I have to restart it and load all the data again (this did it with .c()). Downloading takes 10 to 30 seconds, depending on the model for the likelihood function and the data, and I was wondering if there is a way to save the C ++ application while waiting for function value requests, so I don’t have to read all the data into memory. Already the calculation of a single function value in a C ++ application takes about half a second, which is very long for C ++.

I was thinking of using pipe()to do this, and ask you if this is an acceptable option or to use some other method? Can this be done with rcpp?

I am doing this to check the minimization of algorithms for R for this function.

+4
source share
2 answers

Forget about .C. This is awkward. Perhaps using .Cover .Callor .Externalmade sense before Rcpp. But now with the work that we put in Rcpp, I no longer see the point in using it .C. Just use it .Call.

Even better, with attributes ( sourceCppand compileAttributes), you don’t even need to see anymore .Call, it just seems like you are using a C ++ function.

Now, if I wanted to do something that saves states, I would use a module. For example, your application is a class Test. It has methods do_somethingand do_something_else, and it counts the number of methods 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 ; 
} ;

++. , R, :

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 )
    ;
}

:

app <- new( Test )
app$count
app$do_something()
app$do_something()
app$do_something_else()
app$count
+7

.

++ R?

, Rcpp . .Call R , , Rcpp.

R ++?

. ++, ++, R.

+1

All Articles