1

What Was I Trying to Do?

I was trying to calculate VIF (Variance Inflation Factor) using VIF function of car package of R. In python, to import the car package, I used the importr function of rpy2 as shown below.

from rpy2.robjects.packages import importr
car = importr('car')

Then, What Did Happen?

However, after running the codes in Google Colab, I got the following error.

PackageNotInstalledError: The R package "car" is not installed.

I understand that it is saying that the package car is not installed.

Then, My Question

In Google Colab, I did not need to install any package like Keras, Pandas etc.. In fact, I did not need to install stats package (to use via rpy2) of R. Then, why will I need to install package like car, usdm, mtcars to use via rpy2? Also, I do not know how to install those packages to use through the rpy2 library.

How Did I Try to Solve?

I searched on Google to find the ways to use (via rpy2) those packages (e.g. car, mtcars) in Google Colab. However, I failed to find the ways. It can be noted that I can use those packages (e.g. usdm, car) via rpy2 in Jupyterlab Notebook (after installing). However, I want to use those packages in Google Colab.

Any type of help will be greatly appreciated!

Md. Sabbir Ahmed
  • 850
  • 8
  • 22

4 Answers4

3

Why? Because R can be installed with or without extra packages. Apparently Google Colab contains a minimal installation of R, including only the built-in R packages such as base, utils, stats etc. To re-iterate, these packages are a part of R by default (not on CRAN). Any other packages that you get when installing R are bonus for your convenience; for example in Ubuntu you have r-base and r-recommended; typically one would get both, but system admins who are short on space might decide to only provide the former. See Difference between r-base and r-recommended packages

How? You need to install it with:

from rpy2.robjects.packages import importr

utils = importr('utils')
utils.install_packages('car')
krassowski
  • 13,598
  • 4
  • 60
  • 92
  • 2
    In addition the example in the answer, the doc covers the topic of installing R packages: https://rpy2.github.io/doc/v3.4.x/html/introduction.html#installing-packages – lgautier Mar 24 '21 at 15:52
  • Can you please let me know how to save these installed packages to google drive permanently? In SO, though some answers (e.g. [this one](https://stackoverflow.com/questions/55253498/how-do-i-install-a-library-permanently-in-colab)) already available regarding pip installed packages, however I do not find any answer regarding packages installed via `install_packages()` function. – Md. Sabbir Ahmed Mar 26 '21 at 06:22
1

An alternative solution by devtools(R package) for GitHub repos,

from rpy2.robjects.packages import importr

utils = importr('utils')
utils.install_packages('devtools')
devtools = rpackages.importr('devtools')
devtools.install_github("xxx/xxx")
Jiaxiang
  • 865
  • 12
  • 23
1

I know this question is one year old, but I have had the same issue just now and I have figured out a way to install car in a Colab notebook:

A big problem is that R is not very forthcoming with its error messages in a Colab notebook. The issue for me was two offending dependencies, namely the nloptr-package and the gsl-package, that I had to find through extensive trial and error.

In the end, I had to manually install nloptr version 1.0.4 as well as gsl version 1.2-19 from source. This means you have to download both archives from https://cran.r-project.org/src/contrib/Archive/, copy them to your Google Drive and then install.

I should point out that I am using Python and R simultaneously via cell magic and rpy2.ipython. So in this case, I have to preface every notebook cell that uses R-code with %%R.

Also be mindful that you have to mount your Google Drive to Colab beforehand (in a regular Python cell) to be able to install the R-package from source. Put the two together and you get:

%load_ext rpy2.ipython
from google.colab import files, drive
drive.mount('/content/drive')

Then you can install nlopre and gsl from source and, finally, car from CRAN:

%%R
install.packages("drive/MyDrive/nloptr_1.0.4.tar.gz", repos = NULL, type = "source")
install.packages("drive/MyDrive/src/gsl_1.2-19.tar.gz", repos = NULL, type = "source")
install.packages("car", repos = "https://cloud.r-project.org")
  • I adapted the answer slightly: The correct `loptr`-version is 1.0.4. You also need to install `gsl` version 1.2-19 from source before installing `car`. – Matthias Reccius Mar 10 '22 at 08:26
-1

do pip install with following code in colab.

!pip install packageName

Prem Anand
  • 49
  • 6