2012-07-19 284 views
2

我在我的Python腳本中通過rpy2庫使用R,我需要一個package,它不在R的默認安裝中。我該如何安裝它?R,Python:在rpy2上安裝軟件包

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

將不起作用。

關於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 

而且從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) 

回答

-1

這個怎麼樣

>>> import rpy2.interactive as r 
>>> r.importr("utils") 
>>> package_name = "DirichletReg" 
>>> r.packages.utils.install_packages(package_name) 
+1

不工作了。見Aaron的答案,現在起作用。 – wordsforthewise 2017-12-20 01:13:17

14

很多已經在過去的兩年中變化,裏卡多的答案沒」爲我工作。我建議這個方法從Python的安裝:

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

utils包是R.utils包,其文檔可以在這裏找到:https://CRAN.R-project.org/package=R.utils

正如我上次編輯的,the documentation仍然說要做到這一點。