How to use R and Python in the same notebook?

R & Python

If you are familiar with the data science domain, then you know that R and python are two languages of great importance. Data science has found its applications in almost every industry, from Finance departments to healthcare to marketing and whatnot.

Why do we need these Languages?

In the field of data science, R is the most widely used, open-source language. It is often used for statistical analysis and data visualization of both structured and unstructured data. In comparison to other Data Science languages, R supports a number of features.

Python is an object-oriented, high-level programming language. It is a multi-functional, completely interpreted programming language that has a number of benefits and is frequently used to simplify enormous and complex data collections.

Also read: Jupyter Notebook for Python – Comprehensive Guide

Using R along with Python in Google Colab

Now, Google Colab (Collaboratory) allows anybody to write and execute arbitrary python code through the browser and is especially well suited to machine learning, data analysis, and education.

By default, Colab creates a python notebook, but one can also create an R notebook in Colab. To do so click the below link.

https://colab.research.google.com/#create=true&language=r

To use R and python simultaneously in the same notebook, we need to activate the rpy2 package first.

%load_ext rpy2.ipython
rpy2
rpy2

rpy2 is a high-level interface making R functions an object just like Python functions and providing a seamless conversion to NumPy and pandas data structures. After its activation, the cells get executed normally in python language, and by adding an extra method the code is executed in R language.

The methods are – cell magic and line magic.

1. Cell magic

This is used to run the entire cell in R language. The first line of the cell must be the line given below, after that from the second line we can start writing code in R language and it will get executed successfully.

%%R
......
.....

2. Line magic

This is used to run a singular line of code in R language. The cell can contain other lines of code in python language. The line with the following code will get executed in R.

%R ...........

Basic Implementation

Basic Implementation
Basic Implementation

Installing packages and libraries

The best part of using R and python in the same notebook is that one can use all packages provided by R in Python by importing the libraries right inside the colab notebook. This makes the work of a data scientist or whoever is working on a project much more efficient.

packages
packages

Importing data from one language to another

Consider working on a project which uses both python as well as R language. with rpy2 and magic commands one can convert a data frame from one language to another

Python to R

%R -i df_1
Python to R
Python to R

R to Python

%R -o df_i 
R to Python
R to Python

Summary

R and Python both are widely used languages in the field of data science. Sometimes there might arise a situation when one requires to work with both languages. In this article, we have discussed a solution to this situation. We have learned how to use both languages in a single notebook on the google colab platform.