R & Python

Bruno Lima
Analytics Vidhya
Published in
4 min readAug 18, 2020

It can be great to know how to use both languages in a single file in a efficient way, even more the hacks and insights behind it, through a programming interface that is, in a way that covers all possible cases which aren’t covered by high level interfaces. In the present article, I will explore the rpy2 package in Python, which allows us to use R from Python, which is much more common in online programming services like Google Colab.

Now, let’s focus ourselves in three strategies to perform it, exploring local server and interactive usage, jupyter-notebook and jupyter-lab fit in it. Althoug, batch mode(scripts) and remote servers like Google Colab can be used as well.

First Strategy :

Using API parse mechanism

An inconvenient in this strategy is the fact that Parse API mechanism may fail.

Second Strategy :

Using Custom magics

An inconvenient in this strategy is the inelegance, since it can’t be used comfortablely in a unique cell, that is, it can’t allow us to use both languages in a single cell.

Third Strategy :

Custom input

It requires IPython atualized.

In Google Colab, the atualization can be performed by the following line command:

pip install ipython --upgrade

This setting appears to be great, that is the kind I prefer.

See the following example usage :

This code has the detail that is to implement a solution against the crashing of Python kernel due to the opening image devices in R space. In a simple words, it denotes that embed R and Python interactive processes don’t run simultaneously, only one of them for his time. The idea was to keep R session opened ( R code running) while device is opened, avoiding the existence of a interactive R subprocess ( image device) in Python runtime , after R runtime ( time in which interactive R process is running). This insight is constructive.

From all of this settings, the strategy of communications between these processes is to explore importation and exportation mechanism of both languages and the data frame structure.

Case I — TEXTUAL OUTPUT

Result as the following:

Output was displayed in Terminal. It is the same for Jupyter-lab.

If we want to exhibit text results in notebook/laboratory , It is good to play like this

This step isn’t necessary in Google Colab.

CASE II-Graphic outputs

It was shown how to exhibit images in a opened image device. Let’s show how to exhibit them in laboratory/notebook as well. Just play like this

Then

CASE III-Variables transmission

In general, it is suitable and good to use data frame structure as a intermediate of transmission.

a) From R to Python

Play like this

Then,

b) From Python to R

Play like this

Then, this result arises

[1] 1 2 3 4 5
[1] 1 2 3 4 5

CASE IV- Interactive applications of R

Then,

It shows that we need to take care about to program the finish of an application to be able to stop the R process and then go back to the Python process. Futhermore we need to pay attention in use of the print function to deliver output message to terminal (it isn’t necessarily automatic) and then get results.

In the end, some remarks are essential. The package doesn’t have any support in Windows, at least for while, but this approach allows us to use in that OS. It is due to rpy2 2.9.4 (older version) that is doing well in Windows. You can get that version through anaconda, then you just need to send the package folder to the directory in your usual Python, which is related to pip in my case.

--

--