R, Python: install packages on rpy2

2019-01-23 00:05发布

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)

2条回答
Summer. ? 凉城
2楼-- · 2019-01-23 00:20

How about this

>>> import rpy2.interactive as r
>>> r.importr("utils")
>>> package_name = "DirichletReg"
>>> r.packages.utils.install_packages(package_name)
查看更多
闹够了就滚
3楼-- · 2019-01-23 00:43

Ricardo's answer no longer works.

To install from Python:

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

That utils package is the R.utils package whose documentation can be found here: https://CRAN.R-project.org/package=R.utils

As of my last edit, the documentation still says to do this.

查看更多
登录 后发表回答