How to Install Packages in R Google Colab | by Fidocia Wima Adityawarman | Towards Data Science

In my previous article, I have tried to explain how to run R in Google Colab. In short, there are two ways, first is to use R and Python simultaneously in the same runtime using rpy2, and the second is to run R natively.
towardsdatascience.com
But, there are several limitations when running the R notebook.
The first limitation is related to the version of R used in the runtime. To check what version of R is running at runtime, you can type: version. The limitation I am referring to here is that we cannot choose which version of R to run. Until the time this post was made, the version of R used in Colab notebook is:
platform       x86_64-pc-linux-gnu         arch           x86_64                      os             linux-gnu                   system         x86_64, linux-gnu           status                                     major          4                           minor          0.4                         year           2021                        month          02                          day            15                          svn rev        80002                       language       R                           version.string R version 4.0.4 (2021-02-15)nickname       Lost Library Book
This is different from the Python runtime. You can choose to use Python 2 or Python 3 even though Google is currently trying to stop support for Python 2 on Colab.Python 2 deprecation announcements can be accessed on the Google Colab FAQ page. Here’s what it reads:
Does Colab support Python 2?The Python development team has declared that Python 2 will no longer be supported after January 1st, 2020.  Colab has stopped updating Python 2 runtimes, and is gradually phasing  out support for Python 2 notebooks. We suggest migrating important  notebooks to Python 3.
However, we can still access it via this link https://colab.to/py2.
The second limitation of running R natively relates to Google Drive. Mounting and fetching data from Google Drive and converting it to dataframe is not directly supported if we use R natively in Colab. In my previous article, I have also provided a workaround to retrieve data from Google Drive or BigQuery.
Now, suppose you have successfully run R natively on Colab. Then what libraries or packages are available there? How to install a package that we need, but it’s not available by default? What options are available?
In this post, I’ll try to break that down.
To get a list of libraries that have been installed, you can run this code:
str(allPackage <- installed.packages())allPackage [, c(1,3:5)]
The code above will produce a table like this:
Or you can also run to display a help window that contains information about what packages have been installed at this R runtime:
library()
From there we know that there are some commonly used packages like, for example, dplyr, broom, surival which have been installed by default.
To install packages that are not available by default we could use the install.packages() function, as we usually do.
But there are times when we will find packages that are not available in CRAN, so we may need to install via Github. Or when a newer version of the package is not yet available in CRAN. In that case, we can use the devtools::install_github (“DeveloperName/PackageName”) function.
For example, let’s try installing the “rsvg” package which is required to render SVG Images into PDF, PNG, PostScript, or Bitmap Arrays.
We’ll run the install.packages(“rsvg”) code in our runtime. Wait a few moments, and an error message will appear:
Installing package into ‘/usr/local/lib/R/site-library’(as ‘lib’ is unspecified)Warning message in install.packages("rsvg"):“installation of package ‘rsvg’ had non-zero exit status”
And this, I think, is the third limitation in using R natively in Google Colab.
The message (error or warning) that is displayed is not as complete as when we, for example, use RStudio.
non-zero exit status means that the package was not successfully installed on our system or runtime, and it does not say why.
This incompleteness is a bit troublesome for us in some ways because the information is not clear. We must be forced to find what causes it in search engines.
In the case of the rsvg packages used in the example, it turns out that the problem lies in the existence of a system dependency that is needed but does not exist in the system we are using. R packages often depend on system libraries or other software external to R.
Interaction with the operating system (Ubuntu terminal) in the R runtime can be done by using the system() function.
Continuing the example above with the rsvg package, it turns out that the package requires librsvg2-dev to be installed on our system. To install it, we need to run code like this:
system('sudo apt-get install -y librsvg2-dev', intern=TRUE)
Or, we could use cat() to get relatively prettier result:
cat(system('sudo apt-get install -y librsvg2-dev', intern=TRUE), sep = "\n")
Some of the other R packages may have different dependencies. You can install it in the same way as above. Some other libs you might need are libcurl4-openssl-dev, libgsl0ldbl, gsl-bin, libgsl0-dev, g++, gcc, gfortran-8.
The availability to run R on Google Colab is something to celebrate, despite some limitations. Packages that have been provided by Google Colab may be enough to fulfill some parts of our work in processing data. This post, as well as the previous one, are just examples of how to get around the limitations that exist in R Google Colab. Finally, I hope this article is useful for anyone who needs it.