无法在python(Jupyter笔记本)中安装R包?

时间:2017-09-10 12:18:55

标签: python-3.x jupyter-notebook rpy2

我正在尝试在jupyter笔记本中在python 3x上安装R包。

我知道我必须pip安装rpy2并且它已经成功

当我在R中调用内置函数(例如ccf或其他简单问题)时,这可以正常工作。

# Call function from R
import os
os.environ['R_USER'] = 'D:\Anaconda3\Lib\site-packages\rpy2'
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
pandas2ri.activate()

但是,如果我想安装DirichletRegvars这样的软件包,那就不是那么容易了,尤其是可能需要下载更多的软件包。

我的确遵循了

中描述的链接

R, Python: install packages on rpy2

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

但收到以下RuntimeError

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-16-32acf37e1ef9> in <module>()
      1 from rpy2.robjects.packages import importr
      2 utils = importr('utils')
----> 3 utils.install_packages('DirichletReg')

D:\Anaconda3\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    176                 v = kwargs.pop(k)
    177                 kwargs[r_k] = v
--> 178         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    179 
    180 pattern_link = re.compile(r'\\link\{(.+?)\}')

D:\Anaconda3\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    104         for k, v in kwargs.items():
    105             new_kwargs[k] = conversion.py2ri(v)
--> 106         res = super(Function, self).__call__(*new_args, **new_kwargs)
    107         res = conversion.ri2ro(res)
    108         return res

RRuntimeError: Error in (function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,  : 
py2/R/win-library/3.3'\Anaconda3\Lib\site-packages
有没有人早点发现这个困难?

1 个答案:

答案 0 :(得分:0)

Jupyter Notebook用户(Windows)

1)似乎我所经历的是R库与python库中的目录不在同一个目录

2)似乎有些软件包需要先安装在R中

要解决此问题,需要在R中使用2个主要步骤,在Python Jupyter笔记本中使用另一个步骤

步骤1:转到R(Rstudio)

代码:

install.packages('DirichletReg', dep = TRUE)

这将告诉你

package ‘httpuv’ successfully unpacked and MD5 sums checked
package ‘xtable’ successfully unpacked and MD5 sums checked
package ‘sourcetools’ successfully unpacked and MD5 sums checked
package ‘htmlwidgets’ successfully unpacked and MD5 sums checked
package ‘shiny’ successfully unpacked and MD5 sums checked
package ‘miscTools’ successfully unpacked and MD5 sums checked
package ‘rgl’ successfully unpacked and MD5 sums checked
package ‘maxLik’ successfully unpacked and MD5 sums checked
package ‘DirichletReg’ successfully unpacked and MD5 sums checked

然后在R中加载包

> loadNamespace('DirichletReg')

它将输出为:

<environment: namespace:DirichletReg>

通过R:

编码仔细检查目录
R.home()

检查输出为

"C:/PROGRA~1/R/R-33~1.3"

招!!!

这不是R下载软件包的地方。您可以通过在R:

中编码来查看下载位置
.libPaths()

说结果是XYZ(复制这个)

第2步:转到Jupyter笔记本

检查当前的R目录(我假设你已经安装了rpy2)

import rpy2
import os
os.environ['R_USER'] = 'D:\Anaconda3\Lib\site-packages\rpy2'
from rpy2.robjects.packages import importr
base = importr('base')
print(base.R_home())

输出将是

"C:/Program Files/R/R-3.3.3"

因此与包在XYZ中的R库目录不匹配

因此导入或安装新包所需的全部是

DirichletReg = importr("DirichletReg", lib_loc = "XYZ")

这将会像我一样为所有其他人工作

mi = importr("mi", lib_loc = "XYZ")
ggplot2 = importr("ggplot2", lib_loc = "XYZ")

但它对DirichletReg没有用,它给了我错误

RRuntimeError: Error in loadNamespace(name) : there is no package called 'ggplot2'