Perl Del Rey
Perl Del Rey

Reputation: 1059

Rpy2 on windows

I want to install rpy2 on my windows machine and it gives me the following famous error

      self.convert_pycparser_error(e, csource)
      File "c:\users\96171\appdata\local\temp\pip-install-lxsxk7q4\rpy2\.eggs\cffi-1.14.0-py3.6-win-amd64.egg\cffi\cparser.py", line 336, in convert_pycparser_error
        raise CDefError(msg)
    cffi.CDefError: cannot parse "void __cffi_extern_python_start; void _capsule_finalizer(SEXP); void __cffi_extern_python_stop;"
    <cdef source string>:495:96: Illegal character '\r'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\96171\AppData\Local\Temp\pip-install-lxsxk7q4\rpy2\

I have done the following:

I have followed the link here for installing rpy2 on windows and they're saying we have to download a precompiled binary in order for this to work.

I have downloaded from the followinf link this wheel: rpy2‑2.9.5‑cp37‑cp37m‑win_amd64.whl however I am still facing trouble; I did as follows:

ERROR: rpy2-2.9.5-cp37-cp37m-win_amd64.whl is not a supported wheel on this platform.

in the website for the binaries they say that:

Rpy2 (discontinued, unstable): provide access to the R software environment for statistical computing and graphics. Requires R 3.3, and I have R 3.6.2 on my system. Is this causing the error?

Upvotes: 3

Views: 2544

Answers (1)

Perl Del Rey
Perl Del Rey

Reputation: 1059

OLD ANSWER
The issue was that I downloaded the wrong wheel. cp37 means CPython 3.7 and I have python 3.6 on my system. I downloaded rpy2-2.9.5-cp36-cp36m-win_amd64.whl istead of rpy2-2.9.5-cp37-cp37m-win_amd64.whl and it worked.

Also as I faced problems with R_USERand R_HOME I did the following in my python script:

import os
# see the following 2 lines
os.environ['R_HOME'] = 'C:/Program Files/R/R-3.6.2'
os.environ['R_USER'] = 'C:/Users/96171/AppData/Local/Programs/Python/Python36/Lib/site-packages/rpy2' #path depends on where you installed Python. Mine is the site packages of the regular python installation, it could have been Anaconda

# importing rpy2
import rpy2.robjects as robjects
r = robjects.r
r['source']('sample_r.r')

References: this

UPDATED
To avoid os calls I was able to achieve the desired behavior by including R_USER and R_HOME to the SYSTEM VARIABLE in ENVIRONMENT VARIABLES

  • R_USER: C:\Users\96171\AppData\Local\Programs\Python\Python36\Lib\site-packages\rpy2
  • R_HOME: C:\Program Files\R\R-3.6.2

Upvotes: 2

Related Questions