How to use R in Google Colab?

[This article was first published on coding-the-past, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Introduction


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.





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.




Coding the past: How to use R in Google Colab?


1. 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. Check the figure below.


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.




2. 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.




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.



To leave a comment for the author, please follow the link and comment on their blog: coding-the-past.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)