If you don’t know, on Google Colab you can have free notebooks for programming on the cloud.
All you need is a desire to learn and, of course, a Google account.
The main advantages are:
- You start programming without wasting time on configurations.
- You can use Google’s free (or paid) resources instead of your own computer.
- Sharing your notebooks is as easy as sharing a Google Drive document.
Is Google Colab only for Python programming?
No. You can program in Python and R on Google Colab.
The truth is that the default runtime environment in Google Colab is Python 3 and although we can select R as the runtime environment for our notebook, I don’t recommend it. Read on 🙂
How do I use R in Google Colab?
You can also program in R using the Python 3 runtime environment. This way in the same notebook you can run both, R or Python.
The first step is to install the rpy2 Python package with a properly working version. Yes, there are recent versions where it fails and using version 3.5.1 solves the problems.
Once this is done, remember to include %%R as the first line in every code cell where you want to execute code in R.
How to load an R file from Google Drive on my Google Colab notebook?
Well, this, among other reasons, is the reason why on Google Colab it is preferable to use the Python 3 runtime environment type with the magic %%R IPython command instead of using the R runtime environment directly.
In order to connect a Google Colab notebook to Google Drive we have to use Python 3 code. Once connected, we can now use code in R.
Here is an example, in which we connect to Google Drive, create an R directory and save a hello_world.R file which is then executed.
How do I save the changes now?
If you have edited the notebook I shared with you, you will see that the changes cannot be saved. To do so, save a copy to your Google Drive account.
Should I use Google Colab or not?
One point in favor of using Google Colab is that it already has many packages pre-installed. Yes, the tidyverse packages are useful but if you use RStudio or VS Code on your computer, you have to install them on your computer.
Although for professional environments I would recommend using RStudio or VS Code and many more things to build reproducible processes in R, Google Colab notebooks may have their audience.
And now what?
Well, just to say that there are more Google Colab tricks that are left unexplained and I could publish more in the future if it is a topic of interest. Do you want to share your own tricks? We are all ears.