Open In App

How to use R with Google Colab?

Last Updated : 13 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Google Colab is a cloud based coding platform providing free access to limited resources like GPU or TPU. It comprises cells where one can write codes and execute them. It also provides the facility of sharing in real time. Some of the features are:

  • It allows users to share notebooks in real time.
  • It helps to import external datasets.
  • It helps to upload notebooks.
  • Most of the libraries are available in Google Colab. Hence very few libraries are required to be installed.

Ways to use R with Google Colaboratory

R is a programming language used for data analysis and statistical analysis. It provides a vast amount of libraries and tools that are required for data visualization. We can install the R programming language using the link ‘How to install R‘. However, Google Colab can also be used for R programming. There are two ways to use R with Google Colab.

1. Changing Runtime type

The first way is very simple. Below are the steps to follow:

1. Open the Google Colab and then click on the new notebook.

snap-1-copy

Opening Google Colab

2. When the new Notebook opens click on the Runtime option that is available from the list of options.

snap2-copy-2

Clicking on Runtime

3. Then a dropdown appears. From the dropdown click on Change Runtime Type.

snap2_-copy-2

Click on Runtime type

4. A dialog box appears. Under the heading Runtime type by default Python3 appears. Click on the arrow and change it to R. After that click on Save.

SNAP3-copy

Choose R

5. Execute any command of R. You will get to see the results.

snip2-copy

Executing R commands

In the above output we have created two vectors named vector 1 and vector 2 and created a plot for them.

2. Using rpy2

The second way is useful when you want to use both Python and R. The steps are as follows:

1. Open the Google Colaboratory and then click on the new notebook.

snap-1-copy

2. In the cell execute the command : %load_ext rpy2.ipython.

3. Now whenever you need to use R commands in each cell write %%R at the beginning followed by R commands.

4. After that execute the cell using Ctrl+Enter.

snip3

Executing R

Using these two techniques one can use R on google colab without installing R package.

How to Save and open R file in Google Colab?

Google Colab automatically saves our work. However due to some network issues the work might not get saved properly. If the work is not saved then Colabo issues a warning that the work has not been saved yet. Press Ctrl+S to save your work. For downloading the file follow the steps:

1. Click on file.

2. Click on download and then click on Download ipynb.

snip-4

Download ipynb file

Now whenever you want to upload notebook click on Upload notebook. Then click on the ipynb file that you want to upload. After that change the runtime if required (that is first approach is used) and then execute the cells one by one.



Next Article

Similar Reads