Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rpy2 import is not working

I get an error when I try to import rpy2. Here is the code and error.

>>> import pandas.rpy.common
Traceback (most recent call last):  

File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\IPython\core\interactiveshell.py", line 2828, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-148-c258a0f70d44>", line 1, in <module>
import pandas.rpy.common

File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\pandas\rpy\common.py", line 14, in <module>
from rpy2.robjects.packages import importr

ImportError: No module named 'rpy2'

What could be the issue? I'm using python version 3.3.3 and pandas version 0.13.1

EDIT

Tried to install rpy2 separately.

  1. Directly using python setup.py install gave me an error that os doesn't have a module popen3.

  2. Directly installing the exe (rpy2-2.3.9.win32-py3.3.exe) from Christoph Gohlke's site http://www.lfd.uci.edu/~gohlke/pythonlibs/ run fine. But if I try to do import pandas.rpy.common as com then I get the following error (issue with the loading the DLL at from rpy2.rinterface._rinterface import * :

    Traceback (most recent call last):
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\IPython\core\interactiveshell.py", line 2828, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
    
      File "<ipython-input-10-63ebebefea80>", line 1, in <module>
    import pandas.rpy.common as com
    
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-    packages\pandas\rpy\common.py", line 14, in <module>
    
    from rpy2.robjects.packages import importr
    
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\rpy2\robjects\__init__.py", line 15, in <module>
    
        import rpy2.rinterface as rinterface
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\rpy2\rinterface\__init__.py", line 103, in <module>
    
        from rpy2.rinterface._rinterface import *
    ImportError: DLL load failed: %1 is not a valid Win32 application.
    

EDIT

Solved it finally. It seems like adding R_HOME and R_USER environment variables did the trick.

like image 242
uday Avatar asked Feb 17 '14 20:02

uday


People also ask

Do I need to install R to use rpy2?

rpy2 will typically require an R version that is not much older than itself. This means that even if your system has R pre-installed, there is a chance that the version is too old to be compaible with rpy2. At the time of this writing, the latest rpy2 version is 2.8 and requires R 3.2 or higher.

Does rpy2 work with Python 3?

rpy2 is almost certainly not working with Python 2.6. Older Python versions are even less likely to work. While Python 3.3 should be working, earlier version of Python 3 are not expected to (they might work, they might not - you are on your own). Rpy2 is not expected to work at all with an R version < 2.8.

What is rpy2 in Python?

rpy2 is an interface to R running embedded in a Python process. on Pypi. Questions and issues. Consider having a look at the documentation. Otherwise questions should preferably be asked on the rpy mailing-list on SourceForge, or on StackOverflow.

What version of R does rpy2 use?

First up, install the necessary packages. You must have Python >=3.7 and R >= 4.0 installed to use rpy2 3.5. 2. Once R is installed, install the rpy2 package by running pip install rpy2 .


2 Answers

This might not apply directly to your question, but ever since pandas and rpy2 have upgraded, their interface has changed. In order to set it up, you must now:

from rpy2.robjects import r, pandas2ri

pandas2ri.activate()

Now, in order to change a Pandas dataframe into an R dataframe, one must use pandas2ri.py2ri(), or use pandas2ri.ri2py() if you want change an R dataframe into a Pandas dataframe. Further information can be found at https://pandas.pydata.org/pandas-docs/stable/r_interface.html.

like image 59
Bob McBobson Avatar answered Nov 15 '22 19:11

Bob McBobson


Well, first you have to install rpy2. If you are using conda you can do it using the following command

conda install -c r rpy2 

Then, if you want to use R on Jupyter Notebook environment you can activate R using

%load_ext rpy2.ipython
like image 42
ewalel Avatar answered Nov 15 '22 18:11

ewalel