difficulty installing rpy2 using Python 3.8 via anaconda with R version 4.0.2 on Mac osx
Asked Answered
U

1

6

I have never used R code in python before, but would like to do so in order to implement some Poisson regression models. The reason for this is efficiency (R code for poisson regression much more efficient). I have attempted to install rpy2 by entering the following into my terminal:

conda install -c r rpy2

I have also tried to use pip install with no success. When I attempt to run this in my terminal, it returns the following error in the terminal itself:

Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: / Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort. failed

UnsatisfiableError: The following specifications were found to be incompatible with the existing python installation in your environment:

Specifications:

  • rpy2 -> python[version='2.7.|3.5.|3.6.|>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|3.4.|3.3.*']

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for. When python appears to the right, that indicates that the thing on the left is somehow not available for the python version you are constrained to. Note that conda will not change your python version to a different minor version unless you explicitly specify that.

After using pip install, and running some code referencing rpy2 modules, I received the following errors:

import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector


package_names = ('stats')

if all(rpackages.isinstalled(x) for x in package_names):
    have_package = True

else: 
    have_package = False

if not have_package: 
    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)
    
    packnames_to_install = [x for x in package_names if not rpackages.isinstalled(x)]
    
    if len(packnames_to_install) > 0: 
        utils.install_packages(StrVector(packnames_to_install))

OSError: cannot load library '/Users/name/opt/anaconda3/lib/R/lib/libR.dylib': dlopen(/Users/name/opt/anaconda3/lib/R/lib/libR.dylib, 2): Library not loaded: @rpath/libreadline.6.2.dylib Referenced from: /Users/name/opt/anaconda3/lib/R/lib/libR.dylib Reason: image not found

NameError: name 'rpackages' is not defined

Does anyone know how I can successfully use rpy2 in my python 3.8? It seems like this is a common issue with anaconda, but I have not been able to find any actionable solutions on the web. Any help you can provide is much appreciated!

Uteutensil answered 9/12, 2020 at 22:24 Comment(0)
Y
9

rpy2 is not available in conda's defaults channel. Try:

    conda install -c conda-forge rpy2

which uses the conda-forge channel instead.

EDIT: Actually, I’ve found that the best method is to:

  1. Use the miniconda distribution.

  2. Create a project-specific conda environment.

  3. Activate this environment.

  4. Install all conda packages available in the conda defaults channel.

  5. Then

     pip install rpy2
    

For more details: https://www.anaconda.com/blog/using-pip-in-a-conda-environment

Yukyukaghir answered 10/12, 2020 at 15:29 Comment(2)
What the reason you prefer using pip here?Mallen
Because using conda itself was unstable.Yukyukaghir

© 2022 - 2024 — McMap. All rights reserved.