Search code examples
rimportpython-importrpy2r-package

How to import the R package KernSmooth in Python using Rpy2?


I'm using Python 3.7 and rpy2 2.9.4 and installed the package r-kernsmooth (https://www.rdocumentation.org/packages/KernSmooth/versions/2.23-15) which is available in the anaconda distribution (https://docs.anaconda.com/anaconda/packages/r-language-pkg-docs/) using the conda install r-kernsmooth in terminal.

When I try to import the package 'r-kernsmooth' in Python I get the following error:

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’

And indeed, checking the path /anaconda3/lib/python3.7/site-packages/rpy2/robjects/packages.py there is no package called 'r-kernsmooth'.

I have no problem, however, importing the package 'utils' using the exact same procedure.

I should note I that have previously installed 'KernSmooth' in RStudio and I have no problem using it there.

Is something wrong with my code? Or may the anaconda environment be damaged?


Solution

  • I've found the culprit: There appeared to be some conda packages causing inconsistencies and thus 'KernSmooth' hadn't been installed properly via anaconda in the first place.

    (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
    

    I followed the discussion on GitHub (https://github.com/conda/conda/issues/8490) which pointed to an issue with the latest version of anaconda (as of conda 4.6.9): https://github.com/conda/conda/pull/8444

    Restoring consistency to the anaconda environment by reinstalling the packages listed as inconsistent solved the problem for me:

    conda install package_name
    

    I hope this helps.