无法在 python (Jupyter notebook) 中安装 R 包? [英] Unable to install R package in python (Jupyter notebook)?

查看:43
本文介绍了无法在 python (Jupyter notebook) 中安装 R 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 jupyter notebook 中的 python 3x 上安装 R 包.

I am trying to install R package on python 3x in the jupyter notebook.

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

I understand that I have to pip install rpy2 and it has been successful

当我在 R 中调用内置函数(例如 ccf 或其他简单问题)时,这很好用.

This works fine when I call a built-in function in R such as ccf or other easy issues.

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

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

However, if I want to install a package such as DirichletReg or vars, it is not that easy especially that there might be more packages that are required to be downloaded.

我确实按照

R、Python:在 rpy2 上安装软件包

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

但收到以下运行时错误

---------------------------------------------------------------------------
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:Anaconda3libsite-packages
py2
objectsfunctions.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:Anaconda3libsite-packages
py2
objectsfunctions.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'Anaconda3Libsite-packages

有没有人更早发现这个困难?

did anyone found this difficulty earlier?

推荐答案

Jupyter Notebook Users (Windows)

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

Jupyter Notebook Users (Windows)

1) It seems that what I have been gone through was that the R library was not in the same directory as in the python library

2) 好像有些包需要先在R中安装

2) It seems that some packages need to be installed in R first

第一步:转到R(Rstudio)

Step1: Go to 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 中编码仔细检查目录:

double check the directory by coding in R:

R.home()

检查输出为

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

诡计!!!

这不是 R 下载包的地方.您可以通过在 R 中编码来查看它下载到的位置:

Trick!!!

This is not the place where R is downloading the packages to. You can see where it is downloading to by coding in R:

.libPaths()

说结果是XYZ(复制这个)

Say the outcome is XYZ (copy this)

第 2 步:转到 Jupyter 笔记本

Step 2: Go to Jupyter notebook

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

Check the current R directory (I assume you have rpy2 already installed)

import rpy2
import os
os.environ['R_USER'] = 'D:Anaconda3Libsite-packages
py2'
from rpy2.robjects.packages import importr
base = importr('base')
print(base.R_home())

输出将是

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

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

Hence does not match with the R library directory where packages are in XYZ

因此导入或安装新包所需的一切是

hence to import or install the new package all required is to

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

这通常会像我对所有其他人一样工作

This will usaually work like it work with me for all others

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

但它对 DirichletReg 不起作用,它给了我错误

But it did not work for DirichletReg it gave me the error

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

这篇关于无法在 python (Jupyter notebook) 中安装 R 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆