Jump to content
Wikibooks The Free Textbook Project

Python Programming/Extending with R

From Wikibooks, open books for an open world


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.

importrpy2.robjectsasrobjects
fromrpy2.robjects.packagesimport importr
importnumpy
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 | edit source ]

AltStyle によって変換されたページ (->オリジナル) /