2

In R we can use Rcpp to call a cpp function as the one below:

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP critcpp(SEXP a, SEXP b){
     NumericMatrix X(a);
     NumericVector crit(b);
     int p = XtX.ncol();
          NumericMatrix critstep(p,p);
     NumericMatrix deltamin(p,p);
     List lst(2);
         for (int i = 0; i < (p-1); i++){
            for (int j = i+1; j < p; j++){
               --some calculations
             }
          }
          lst[0] = critstep;
          lst[1] = deltamin;
          return lst;
}

I want to do the same thing in python. I have gone through Boost,SWIG etc but it seems complicated to my newbie Python eyes. Can the python wizards here kindly point me in the right direction. I need to call this C++ function from inside a Python function.

4
  • What did you struggle to do with boost.python (I say this because I personally find it the best for this kind of thing) Commented Oct 6, 2017 at 11:06
  • 1
    This function is literally full of Rcpp types, that are not available for python right out of the box. This function must be completely rewritten to be used with python AFAIK. Commented Oct 6, 2017 at 11:06
  • It is possible to bridge in some way R and Python through C++, Rcpp and boost.python, but I don't see the advantages... gallery.rcpp.org/articles/rcpp-python Commented Oct 6, 2017 at 11:07
  • I'd say one of the easier ways would be wrap your C++ function into C callable (convert c++ class instances into c compatible types), then use cdll python library to call it from Python code. Commented Oct 6, 2017 at 11:10

1 Answer 1

1

Since I think the only real answer is by spending some time in rewriting the function you posted, or by writing a some sort of wrapper for the function (absolutely possible but quite time consuming) I'm answering with a completely different approach...

Without passing by any sort of compiled conversion, a really faster way (from a programming time point of view, not in efficiency) may be directly calling the R interpreter with the module of the function you posted from within python, through the python rpy2 module, as described here. It requires the panda module, to handle the data frames from R.

The module to use (in python) are:

import numpy as np  # for handling numerical arrays
import scipy as sp  # a good utility
import pandas as pd  # for data frames
from rpy2.robjects.packages import importr  # for importing your module
import rpy2.robjects as ro  # for calling R interpreter from within python
import pandas.rpy.common as com  # for storing R data frames in pandas data frames.

In your code you should import your module by calling importr

importr('your-module-with-your-cpp-function')

and you can send directly commands to R by issuing:

ro.r('x = your.function( blah blah )')
x_rpy = ro.r('x')
type(x_rpy)
# => rpy2.robjects.your-object-type

you can store your data in a data frame by:

py_df = com.load_data('variable.name')

and push back a data frame through:

r_df = com.convert_t_r_dataframe(py_df)
ro.globalenv['df'] = r_df

This is for sure a workaround for your question, but it may be considered as a reasonable solution for certain applications, even if I do not suggest it for "production".

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.