Member-only story
Google Colab 101: Using Both R & Python
Undeniably, best practice is to only use one language in a notebook, particularly when collaborating with others. However, if you’re familiar with both R & Python you’re bound to come across moments when you wish you could do something in the other language whether it’s because you can do something in one language but not the other, or you can accomplish it in the given language but it’s much simpler to do in the other.
Fortunately, Colab allows you to use both R & Python in the same notebook!
(FYI — if you just want to use R you can create a Colab R notebook through this link: https://colab.research.google.com/#create=true&language=r)
Getting Started
To start, run this code to load R:
%load_ext rpy2.ipython
Running R Code: Basics
Afterwards, all cells will be Python cells by default but you can run R code using two different methods: cell magic & line magic.
Cell magic: running an entire cell as R code. To run cell magic, place the following code in the first line of the cell. All code within the cell will then be run as R code.
%%R
Line magic: running a single line within a cell as R code. Place the following code within a…