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

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

问题描述

我正在尝试在jupyter笔记本中的python 3x上安装R软件包.

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

我了解我必须通过pip安装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:\Anaconda3\Lib\site-packages\rpy2'
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')

但收到以下RuntimeError

but received the following 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

有人早点发现这个困难吗?

did anyone found this difficulty earlier?

推荐答案

Jupyter Notebook用户(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

然后以

> 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:\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库目录不匹配

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笔记本)中安装R包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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