如何使用Rpy2在Python中导入R软件包KernSmooth?

时间:2019-06-05 17:38:55

标签: r import python-import rpy2 r-package

我正在使用Python 3.7和rpy2 2.9.4,并使用conda install r-kernsmooth安装了r-kernsmooth(https://www.rdocumentation.org/packages/KernSmooth/versions/2.23-15)软件包,该软件包可在anaconda发行版(https://docs.anaconda.com/anaconda/packages/r-language-pkg-docs/)中使用。终端。

当我尝试在Python中导入软件包“ r-kernsmooth”时,出现以下错误:

import rpy2.robjects.packages as rpackages
kernsmooth = rpackages.importr('r-kernsmooth')
---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-29-3b8fa4366a16> in <module>
      1 import rpy2.robjects.packages as rpackages
----> 2 utils = rpackages.importr('r-kernsmooth')

/anaconda3/lib/python3.7/site-packages/rpy2/robjects/packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_check_after, data)
    451     if _package_has_namespace(rname, 
    452                               _system_file(package = rname)):
--> 453         env = _get_namespace(rname)
    454         version = _get_namespace_version(rname)[0]
    455         exported_names = set(_get_namespace_exports(rname))

RRuntimeError: Error in loadNamespace(name) : there is no package called ‘r-kernsmooth’

确实,检查路径/anaconda3/lib/python3.7/site-packages/rpy2/robjects/packages.py中没有名为“ r-kernsmooth”的软件包。

但是,使用完全相同的过程导入软件包“ utils”没有问题。

我应该注意,我以前已经在RStudio中安装了“ KernSmooth”,并且在那里使用它没有问题。

我的代码有问题吗?还是水蟒环境会被破坏?

1 个答案:

答案 0 :(得分:0)

我找到了罪魁祸首: 似乎有一些 conda软件包引起了不一致,因此首先没有通过anaconda正确安装'KernSmooth'。

(base) Sebastians-MacBook-Pro:~ sebastiangsell$ conda update r-kernsmooth
Collecting package metadata: done
Solving environment: - 
The environment is inconsistent, please check the package plan carefully
The following packages are causing the inconsistency:

  - defaults/osx-64::jupyterlab_launcher==0.13.1=py37_0
  - defaults/osx-64::nbconvert==5.4.0=py37_1
  - defaults/osx-64::jupyterlab==0.35.3=py37_0
  - defaults/osx-64::jupyter==1.0.0=py37_7
  - defaults/osx-64::ipywidgets==7.4.2=py37_0
  - defaults/osx-64::notebook==5.7.4=py37_0
  - defaults/osx-64::spyder==3.3.2=py37_0
  - defaults/osx-64::jupyterlab_server==0.2.0=py37_0
  - defaults/osx-64::widgetsnbextension==3.4.2=py37_0
  - defaults/osx-64::_ipyw_jlab_nb_ext_conf==0.1.0=py37_0
  - defaults/osx-64::xlwings==0.15.1=py37_0

我关注了GitHub(https://github.com/conda/conda/issues/8490)上的讨论,该讨论指出了最新版本的anaconda(自 conda 4.6.9 起)存在问题: https://github.com/conda/conda/pull/8444

通过重新安装列为不一致的软件包来恢复anaconda环境的一致性为我解决了该问题:

conda install package_name

我希望这会有所帮助。