R,Python:在 rpy2 上安装包

2022-01-13 00:00:00 python r package import

问题描述

我通过 rpy2 库在我的 Python 脚本中使用 R,我需要一个 package 不在 R 的默认安装中.如何安装?

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

不会工作.

在 Python 上:

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

来自 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)


解决方案

这个怎么样

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

相关文章