Contents

Change Google colaboratory default language as R

Note
Without doing below, you may see a GUI menu on Colab;
Runtime > Change runtime type, then change the type of runtime into R.
I’m not sure what enables this settings because some notebooks seem not have options for changing the type of runtime. If you do not see the options, below procedure would be an alternative.

Option1. Create notebook with R language from URL endpoint

Below link enables blank noteboook with default kernel language R.

https://colab.research.google.com/notebook#create=true&language=r

Option2. Change notebook kernel spec on Google Colab

For setting Google Colaboratory default language as R (from python), I tweaked the .ipynb file as below.

  1. Open Google colaboratory project page (.ipynb)
  2. File > Download > Download .ipynb
  3. Open the ipynb by text editor (in my case I used vim editor on Mac terminal) and change below lines:

Before

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Project_r.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    ...
}

After

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Project_r.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "ir",
      "display_name": "R"
    },
    ...
}
  1. Upload the changed .ipynb to the Google Colaboratory page.

Then you can call R command on Google colaboratory without typing %%R before every execution.

Then you can run R command and packages on .ipynb file like:

1
library(tidyverse)

References