【问题标题】:Unable to install R package in python (Jupyter notebook)?无法在 python (Jupyter notebook) 中安装 R 包?
【发布时间】:2018-02-18 19:19:50
【问题描述】:

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

我知道我必须 pip install 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

之前有没有人发现这个困难?

【问题讨论】:

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


    【解决方案1】:

    Jupyter 笔记本用户 (Windows)

    1)看来我经历的是R库与python库不在同一目录中

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

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

    Step1:转到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'
    

    【讨论】:

      猜你喜欢
      • 2017-07-16
      • 2018-07-20
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多