1

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

J.D.
  • 139
  • 4
  • 14
  • There's been a decent number of views on this question, but no responses. Even if you don't know the answer, may I at least have a response as to why no one has answered so I know how to improve the question? Thanks – J.D. Jan 13 '20 at 07:40
  • have you installed R? – adatzer Jan 23 '20 at 08:09
  • Yes, I have. On my desktop, I have R i386 3.6.2 and R x64 3.6.2 – J.D. Jan 23 '20 at 08:14
  • in the Control Panel look for environment variables(probably Settings > Advanced System Settings > (Advanced Tab) Environment variables). Check there, are there any R-related variables set? – adatzer Jan 23 '20 at 08:31
  • There are just 4 environment variables there: OneDrive, Path, TEMP and TMP – J.D. Jan 23 '20 at 08:48
  • See, there are the user and the system environment variables and you can create new. For start, try following the instructions in the top answer here (adjust r-version and your user name): https://stackoverflow.com/questions/12698877/how-to-setup-environment-variable-r-user-to-use-rpy2-in-python Does that work? – adatzer Jan 23 '20 at 09:21
  • Just to be sure, to **add** to Path, for example if Path was `C:\Program Files (x86)\Folder` change it `C:\Program Files (x86)\folder;C:\Program Files\R\R-3.6.2\bin\x64` – adatzer Jan 23 '20 at 09:39
  • @adatzer Thanks for your responses. So I didn't exactly follow that link's instructions to the letter, but I think I did something close enough. First, I went to Windows' environment variables and added R_HOME (value C:\Program Files\R\R-3.6.2) and R_USER (value C:\Users\(my name)\Anaconda3\Lib\site-packages\rpy2 Then, I created a new .py file with just the following code – J.D. Jan 23 '20 at 15:00
  • from rpy2.robjects import r print(r(''' # create a function `f` f <- function(r, verbose=FALSE) { if (verbose) { cat("I am calling f().\n") } 2 * pi * r } # call the function `f` with argument value 3 f(3) ''')) That returned nothing. But putting a print() around it yielded [1] 18.84956. So far so good. But when I then attempted to run my own program, I got the following error: RRuntimeError: Error in file(file, "rt") : cannot open the connection – J.D. Jan 23 '20 at 15:01
  • i'm new SO user, but this seems like another question (got warnings for too many comments). So i'm gonna attempt a proper answer below, including some links to your RRuntimeError that might help. – adatzer Jan 23 '20 at 16:45

1 Answers1

0

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:

adatzer
  • 556
  • 1
  • 4
  • 6