How to use R in Google Colab?

Bruno Ponne ● 03 Oct 2023


  • Easy
  • R
  • 5 min

What you will learn

  • Get a foundational understanding of Google Colab's concept and capabilities;
  • Learn a step-by-step on setting up R within Google Colab;
  • Follow a hands-on example of online coding using the 'babynames' R package,

Table of Contents


Greetings, humanists, social and data scientists!


Ever imagined a world where any computer with an internet browser could be your playground for programming in R? Believe it, because this is your reality! Google Colab enables you to execute R code with remarkable ease, eliminating the need for installations and providing a seamless setup process. The cherry on top? It’s free and allows easy sharing and collaboration on your projects.


In this lesson, I will show you how to use R in Google Colab and suggest a code example to explore the popularity of baby names over the last century. Don’t forget to check out the step-by-step tutorial in the video below! Additionally, feel free to copy the example code from the sections that follow.





1. Data source

The data used in this tutorial comes from the babynames package. It contains a dataset with baby names registered between 1880 and 2017 in the United States. For further details, please explore the documentation of the package here.




2. Set the right runtime

It is actually quite straightforward to configure Google Colab to accept R code. All you have to do is change the runtime type from Python 3 to R. In the menu Runtime, choose the option Change Runtime Type, as shown in the figure below. That’s all!


Figure explaining how to use R in Google Colab

tips_and_updates  
You can also set which type of hardware accelerator you would like to use to process your code. Usually graphics processing units (GPU) are better for machine learning applications.




3. Example: using R in Google Colab

Below you find the code used in the video tutorial. Feel free to copy, test and change it!


content_copy Copy

install.packages("babynames")

library(babynames)

str(babynames)

bruno_df <- babynames[babynames$name == "Bruno", ]

plot(x = bruno_df$year, 
    y = bruno_df$n,
    type = "l",
    main = "Popularity of the name Bruno")



tips_and_updates  
Note that each time you reopen a Colab notebook, you will need to reinstall the libraries required to run your code. While this might seem like a disadvantage, it also encourages you to code without relying heavily on numerous libraries, potentially leading to more robust code.


Leave any questions or concerns you might have in the comments below.




4. Conclusions


  • Google Colab provides a hassle-free journey into the world of R programming and data exploration;
  • with a small change in Google Colab runtime type, it can proccess R code
  • mastering the use of R within Google Colab not only enriches our data analysis skills but also paves the way for enhanced collaborative computing in the cloud.



Comments

There are currently no comments on this article, be the first to add one below

Add a Comment

If you are looking for a response to your comment, either leave your email address or check back on this page periodically.