简体   繁体   中英

Import rpy2.robjects gives error

I have a data which contains 10 millions records. I have an R code which requires to estimate the coefficient of a model using 3000 iterations. Running the R code on this data is very time consuming and sometimes my system got hang. I am using windows 8.1-64 bit version with 4 GB ram. In order to reduce the time, I want to integrate R with Python. Though I have moderate knowledge in R, but I am completely new in Python. I found out that rpy2 can be used to call R from python (I have python version 3.4.1). I have done the following:

import rpy2

import rpy2.robjects as robjects

But is is giving the following error:

Traceback (most recent call last): File "C:\\Python34\\lib\\site-packages\\rpy2\\rinterface__init__.py", line 29, in 0, win32con.KEY_QUERY_VALUE ) pywintypes.error: (2, 'RegOpenKeyEx', 'The system cannot find the file specified.')

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in import rpy2.robjects as robjects File "C:\\Python34\\lib\\site-packages\\rpy2\\robjects__init__.py", line 15, in import rpy2.rinterface as rinterface File "C:\\Python34\\lib\\site-packages\\rpy2\\rinterface__init__.py", line 32, in except ImportError(ie): NameError: name 'ie' is not defined

I cannot understand why I am getting error. How to overcome the error.

But, if I do the following, its working:

from rpy2 import *

It will be very helpful if someone explain how to call R from Python elaborately and give a solution for my problem. Any other solution regarding how to run big data in R with lesser time will also be appreciated. Thanks in advance!

Ran into the exact same problem running in Win7 x64 with Python 2.7 and R3.1.1

1) install pywin32: http://sourceforge.net/projects/pywin32/
2) Add the environment variable R_HOME: [path to R, not /Rx.x/bin]

I still have an issue with "Unable to unlink tempfile" at import time, but I was able to complete some of the demos outlined here: http://rpy.sourceforge.net/rpy2/doc-dev/html/introduction.html

I had a similar problem due to the fact my environment was not properly setup and import win32api would raise ImportError: DLL load failed: The specified module could not be found . In fixed it by adding to my PATH the folder containing my python.exe .

Note that the exception NameError: name 'ie' is not defined is a bug in rpy2 that hides the actual exception, triggered by the failure to import either win32api or win32con .

However I had another exception afterwards ( RuntimeError: R_HOME not defined ), so I ended up adding a R_HOME variable (= C:\\Program Files\\R\\R-3.1.2 ) as suggested in another answer (and I'm also having the Unable to unlink tempfile warning, which I guess has not been fixed yet, see rpy2 windows Unable to unlink tempfile and https://bitbucket.org/lgautier/rpy2/issue/132/rpy2-windows-unable-to-unlink-tempfile )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM