How to Use R with Google Colab

As a software engineer its important to have the right tools in your arsenal to get the job done One such tool is Google Colab a free online platform that allows you to run Jupyter notebooks and access powerful computing resources While Google Colab is primarily geared towards Python users its also possible to use R with it In this blog post well cover how to use R with Google Colaboratory

What is Google Colab?

Google Colab, or Colab for short, is an online platform that provides free access to Jupyter notebooks, which allow you to write and run code in a web browser. Colab is hosted on Google’s cloud infrastructure, which means you have access to powerful computing resources without needing to set up any hardware or software on your local machine.

Colab also comes with a number of pre-installed libraries and packages, making it easy to get started with data analysis, machine learning, and other computational tasks.

Setting Up R in Google Colab

While Colab is primarily geared towards Python users, it’s also possible to use R with Colab. There are 2 ways you can set up R in Google Colab:

Changing the Runtime Type:

  1. open a new Google Colab notebook by navigating to https://colab.research.google.com/ and clicking on “New Notebook”.

  2. Navigate to the Runtime dropdown menu and select Change Runtime Type.

  3. Change the runtime type from Python to R.

Installing R with Python:

This method is very benefecial if you want to use both Python and R at the same time. The steps for this are as follows:

  1. open a new Google Colab notebook by navigating to https://colab.research.google.com/ and clicking on “New Notebook”.

  2. Run rmagic by executing this command %load_ext rpy2.ipython

  3. After that, every time you want to use R, add %%R in the beginning of each cell. For example:

%%R
x <- 42
print(x)

Once you’ve created your R notebook, you’ll need to install any additional packages or libraries you need to use in your code. To do this, you can use the install.packages() function in R.

For example, if you wanted to install the tidyverse package, you could run the following code:

install.packages("tidyverse")

Uploading Data to Google Colab

One of the benefits of using Colab is that you can easily upload data to the platform and access it from your notebook. To upload data to Colab, you can use the file upload feature.

  1. Click on the “Files” tab in the left-hand panel.
  2. Click on the “Upload” button.
  3. Select the file you want to upload.

Once you’ve uploaded your data, you can access it from your R notebook using the file path. For example, if you uploaded a CSV file called data.csv, you could read it into R using the following code:

data <- read.csv("/content/data.csv")

Running R Code in Google Colab

Once you’ve set up your R notebook and installed any necessary packages and uploaded your data, you’re ready to start writing and running R code.

Colab provides an interactive coding environment that allows you to write and run code in cells. To run a cell, you can click the “Play” button next to the cell or use the keyboard shortcut “Shift+Enter”.

For example, you could run the following code to create a scatter plot of the mpg and hp variables in the mtcars dataset:

library(ggplot2)

ggplot(mtcars, aes(x=mpg, y=hp)) +
  geom_point()

Saving Your R Notebook in Google Colab

Once you’ve completed your analysis or project in Colab, you’ll want to save your R notebook so you can access it later or share it with others.

To save your R notebook in Colab, you can click on “File” in the top left corner and select “Save” or use the keyboard shortcut “Ctrl+S”. Colab will save your notebook to your Google Drive account, where you can access it later.

Conclusion

Google Colab is a powerful platform for data analysis, machine learning, and other computational tasks. While it’s primarily geared towards Python users, it’s also possible to use R with Colab. By following the steps outlined in this blog post, you can set up an R notebook in Colab, install packages and libraries, upload data, write and run R code, and save your notebook for future use. With Colab, you have access to powerful computing resources without needing to set up any hardware or software on your local machine, making it an ideal platform for data scientists, software engineers, and anyone else working with data and code.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.