R, Python: install packages on rpy2

Viewed 23220

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)
3 Answers

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")
Related