Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Working with R in Python

R Python Min

Let’s learn to work with R in Python. Throughout history, two languages have always been competing to better themselves in Data Analysis in the Data Science world.

With these both namely being, R and Python.

Both of these programming languages have their own fanbase, and each to their own has its advantages as well.

With R providing larger support for statistical analysis, and specialization in it, while Python provides an object-oriented approach and a staggering number of integrations with other modules.

The advantages and disadvantages of both Python and R can become a powerful duo when combined. Because where Python lacks, R overpowers and vice versa.

And hence, developers created the rpy2 library which is our topic for the day.

What does this mean for developers who need a combination of both? An opportunity.

Installing the rpy2 module

A prerequisite to starting out is that the rpy2 module will only work if you already have the required version of R installed.

Much like any other module, the rpy2 module would require an installation through the pip package installer for your Python distribution.

In pip, the command for installing rpy2 is simply,

pip install rpy2

This should automatically install the required module, and we can move on to work with it inside a Python script!

In case you wish to test out the features of rpy2 before getting your feet system wet, you can try to work with the docker image first, check out rpy2’s docker hub.

Using R in Python with the rpy2 module

In order to use R in Python, we’ll first import rpy2 into the code.

import rpy2
from rpy2 import robjects

Now, we can start working with R in Python. But, before you get into working with the best of both worlds, it would provide to be useful to look into slight differences in the utilization of the R language in this module.

1. Importing packages through rpy2

A large deal of working with R has to do with importing packages for Data Analysis. And rpy2 provides us with this, through the py2.robjects.packages.importr() function.

This function serves as a method to import packages designed for R into Python, where we can work with them to essentially have the features of both the languages present in the script.

from rpy2.robjects.packages import importr
# imports the base module for R.
base = importr("base")

# imports the utils package for R.
utils = importr("utils")

We can now work with the functions that have been imported through this method.

2. Working with R in Python

The method of working with R in the script is to use the robjects.r instance, which allows us to essentially use the R console.

In case you’re wondering how exactly that works, it’s because the rpy2 module is running an embedded R backstage.

# Essentially retrieving the value of pi in the R console
pi = robjects.r['pi']

# Output : 3.14159265358979

While this approach may work for a single line of code. It is worth a mention that it isn’t a viable approach if we wish to work with a huge piece of code that we need processed in R.

Fortunately, we can input an entire block of code in three quotation marks.

        # create a function `f`
        f <- function(r, verbose=FALSE) {
            if (verbose) {
                cat("I am calling f().\n")
            2 * pi * r
        # call the function `f` with argument value 3
# The result of the function is returned to the Python Environment

The function itself is still present in the R Global Environment but can be accessed with the command, robjects.globalenv['f'], where f is the variable in our R environment.

The rpy2 module provides us with a lot of functionality, and while it may look a little hard in the start, it’s mostly just syntax mentioning the R environment.

Here’s a few examples working with the different features in R!

# Working with different kinds of vectors
res1 = robjects.StrVector(['abc', 'def'])
res2 = robjects.IntVector([1, 2, 3])
res3 = robjects.FloatVector([1.1, 2.2, 3.3])

# Output : c("abc", "def")

# Output : 1:3

# Output : c(1.1, 2.2, 3.3)

# Working with different functions of R
rsort = robjects.r['sort']
res4 = rsort(robjects.IntVector([1,2,3]), decreasing=True)

# Working with matrices in R
v = robjects.FloatVector([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
m = robjects.r['matrix'](v, nrow = 2)
# Output :
#       [,1] [,2] [,3]
# [1,]  1.1  3.3  5.5
# [2,]  2.2  4.4  6.6

# Working with Graphics in R
r = robjects.r

x = robjects.IntVector(range(10))
y = r.rnorm(10)


r.layout(r.matrix(robjects.IntVector([1,2,3,2]), nrow=2, ncol=2))
r.plot(r.runif(10), y, xlab="runif", ylab="foo/bar", col="red")

3. Moving forward

The implementation of working with two different languages to work on solutions to problems opens a lot of doors to new discoveries.

Going forward with working with R in Python would be to use the functionality provided by Python to work with various different modules and extend functionality in the fields of Data Science and Mathematical Logic.

Integrating Pandas, OpenCV, and Scikit-Learn into the program would be worth looking into in order to extend and test out new ideas, without any barrier in features provided by the language.

If you find yourself confused whether a certain feature is available to work with in the rpy2 module, feel free to browse through their well maintained documentation!

Alternatives to rpy2

While the rpy2 is a great module, you might wish to look into other modules in order to check out which one suits you the best.

So, here’s a list to help you figure out which module you require rather than find workarounds for features that do not exist or are not suited to your liking in rpy2.


Now that you know what the rpy2 module provides, and how to set it up to get started with working on your code, you can set sail to compute without a worry about the fight between R and Python.

After all, they’re both on your side now!

Look into our other works with the other modules on pandas, and matplotlib, on your journey with Mathematics and Data Science.