Python Programming/Extending with R


It is possible to call R functions and even modules direcly from Python. One easy way to do this is the rpy2 interface. In the following code snippet, an R module called preprocessCore from the bioconductor portal is loaded and the quantile normalization function is applied on a matrix that is created in python and then converted back to python again.

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import numpy

preprocessCore = importr('preprocessCore')

matrix = [ [1,2,3,4,5], [1,3,5,7,9], [2,4,6,8,10] ]
v = robjects.FloatVector([ element for col in matrix for element in col ])
m = robjects.r['matrix'](v, ncol = len(matrix), byrow=False)
Rnormalized_matrix = preprocessCore.normalize_quantiles(m)
normalized_matrix = numpy.array( Rnormalized_matrix)

It is thus possible to handle R modules and objects.

See also

edit