15

I'm using R in my Python script through the rpy2 library and I need a package that is not in the default installation of R. How can I install it?

install.packages("DirichletReg", repos="http://r-forge.r-project.org")

won't work.

On Python:

>>> install.packages("DirichletReg", repos="http://r-forge.r-project.org") 
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'install' is not defined

And from R:

> install.packages("DirichletReg", repos="http://r-forge.r-project.org")
Installing package(s) into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
  package ‘DirichletReg’ is not available (for R version 2.14.1)
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185

3 Answers3

48

Ricardo's answer no longer works.

To install from Python, we can use the utils.install_packages function:

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

(That utils package is the R.utils package whose pdf documentation can be found here: https://CRAN.R-project.org/package=R.utils - or, more directly here, is the more verbose install.packages function documentation that we use: https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/install.packages. It is renamed to install_packages in Python because the . is not part of a legal Python name as it is in R.)

Next, you need to decide which repo to get the package from.

You can declare the repo when calling utils.install_packages with the repos argument:

utils.install_packages('DirichletReg', repos="https://cloud.r-project.org")

Or you can set the mirror before calling utils.install_packages with

utils.chooseCRANmirror(ind=1) # select the first mirror in the list

or

utils.chooseBioCmirror(ind=1) # select the first mirror in the list

otherwise Python/R will attempt to launch the interactive mirror selector (which may or may not work with your setup).

And then, for a single package:

utils.install_packages('DirichletReg')

Or for multiple packages, pass it a character vector:

from rpy2.robjects.vectors import StrVector

package_names = ('ggplot2', 'hexbin')
utils.install_packages(StrVector(package_names))

These examples were adapted from the rpy2 documentation and the install.packages documentation - and as of my last edit, the documentation still says to do this.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • 1
    I prefer to give the 'repos' arg as well, since in jupyter notebook last time I tried the popup menu to choose a mirror didn't work: `utils.install_packages('gutenbergr', repos='https://cloud.r-project.org')` – wordsforthewise Nov 14 '19 at 17:39
  • This no longer works with rpy 3.5.5: `NotImplementedError: Conversion 'rpy2py' not defined for objects of type ''` – wordsforthewise Oct 31 '22 at 19:13
5

When running pytest, Aaron's answer makes my Python hang and R keep giving error messages, probably because of this:

Calling install_packages() without first choosing a mirror will require the user to interactively choose a mirror.

According to rpy2 documentation, I used this which worked:

from rpy2 import robjects
import rpy2.robjects.packages as rpackages

utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
utils.install_packages("DirichletReg")
DirichletReg = rpackages.importr("DirichletReg")
loikein
  • 129
  • 1
  • 7
-4

How about this

>>> import rpy2.interactive as r
>>> r.importr("utils")
>>> package_name = "DirichletReg"
>>> r.packages.utils.install_packages(package_name)
Ricardo Villamil
  • 5,031
  • 2
  • 30
  • 26