Reputation: 169
I'm trying to run a Python program that uses rpy2. Installation was successful, I think. But when I try to run it, I get the following error:
Traceback (most recent call last):
File "", line 1, in runfile('C:/Users/(file location)', wdir='C:/Users/(file location)')
File "C:\Users(my name)\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)
File "C:\Users(my name)\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/(file location)", line 5, in from rpy2.robjects import r
File "C:\Users(my name)\Anaconda3\lib\site-packages\rpy2\robjects__init__.py", line 19, in from rpy2.robjects.robject import RObjectMixin, RObject
File "C:\Users(my name)\Anaconda3\lib\site-packages\rpy2\robjects\robject.py", line 6, in rpy2.rinterface.initr()
File "C:\Users(my name)\Anaconda3\lib\site-packages\rpy2\rinterface__init__.py", line 208, in initr _initr(r_preservehash=r_preservehash)
RuntimeError: R_USER not defined.
I found a question that addresses just this problem: Rpy2 error wac-a-mole: R_USER not defined. The answer says to set the R_user environment variable. Unfortunately, I don't know how to do that; I hadn't even heard of an environment variable before until this. I followed the only lead I saw: the link in the answer. That led to a webpage with two downloadable python files: set_Rvars32_win32.py and setup.py. I tried running both, but they both yield an error. Here is the traceback for set_Ravars32_win32.py
Traceback (most recent call last):
File "", line 1, in runfile('C:/Users/(my name)/Anaconda3/set_Rvars_win32.py', wdir='C:/Users/Jin/Anaconda3')
File "C:\Users(my name)\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)
File "C:\Users(my name)\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/(my name)/Anaconda3/set_Rvars_win32.py", line 4, in import _winreg
ModuleNotFoundError: No module named '_winreg'
setup.py yields the same error and the same traceback sequence (except of course the file is C:/Users/(my name)/Anaconda3/setup.py)
I then tried running the Anaconda Prompt (Anaconda3).
pip install _winreg yields
ERROR: Invalid requirement: '_winreg'
pip install winreg yields
Collecting winreg
ERROR: Could not find a version that satisfies the requirement winreg (from versions: none)
ERROR: No matching distribution found for winreg
Am I on the right track? Or is there an easier way to set environment variables?
Thanks
Upvotes: 1
Views: 1881
Reputation: 576
It seems that you have to tweek your environment variables manually. To do this, go to the environment variables (Control Panel > System Settings > Advanced System Settings > (Advanced Tab) Environment Variables) and follow the steps from the top answer here: How to setup environment variable R_user to use rpy2 in python . (Make sure you add to the Path).
Concerning the RRuntimeError you mentioned in the comments, it's hard to tell without further info, but it seems like your code tries to open a file (or install R libraries). Check out these links:
Upvotes: 0