Search code examples
pythonrgoogle-colaboratoryrpy2

In Google Colab, How to Import Package Like car, mtcars Using importr?


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!


Solution

  • 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')