28

I'm trying to install rpy2 on my computer, but I wasn't able to do it.

I downloaded the source package and I tryed to install Rpy2 (rpy2-2.3.2) using the command python setup.py install. This is the result:

running install
running build
running build_py
running build_ext
"C:\PROGRA~1\R\R-215~1.2\bin\R" CMD config --ldflags
"C:\PROGRA~1\R\R-215~1.2\bin\R" CMD config --cppflags
"C:\PROGRA~1\R\R-215~1.2\bin\R" CMD config LAPACK_LIBS

returned an empty string.

"C:\PROGRA~1\R\R-215~1.2\bin\R" CMD config BLAS_LIBS

returned an empty string.

Configuration for R as a library:
    include_dirs: ('C:/PROGRA~1/R/R-215~1.2/include/i386', 'C:/PROGRA~1/R/R-215~1.2/include')
    libraries: ('R',)
    library_dirs: ('C:/PROGRA~1/R/R-215~1.2/bin/i386',)
    extra_link_args: ()
   # OSX-specific (included in extra_link_args)
    framework_dirs: ()
    framework: ()
building 'rpy2.rinterface._rinterface' extension
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
creating build\temp.win32-2.7\Release\rpy
creating build\temp.win32-2.7\Release\rpy\rinterface
C:\Rtools\gcc-4.6.3\bin\gcc.exe -mcygwin -mdll -O -Wall -DWin32=1 -I.\rpy\rinterface -IC:\Python27\include -IC:\Python27\PC -IC:/PROGRA~1/R/R-215~1.2/include/i386 -IC:/PROGRA~1/R/R-215~1.2/include -c .\rpy\rinterface\_interface.c -o build\temp.win32-2.7\Release\.\rpy\rinterface\_rinterface.o
cc1.exe: error: unrecognized command line option '-mcygwin'
error: command 'gcc' failed with exit status 1

I also tried with build install and easy_install without success. I have R 2.15.2, Python 2.7.2 and Windows 7 (32 bit).

What am I doing wrong?

PS.if I paste that command, the result is:

cygwin warning:

MS-DOS style path detected C:\PROGRA~1\R\R-215~1.2\bin\config.sh
Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-215~1.2/bin/config.sh
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames

-LC:/PROGRA~1/R/R-215~1.2/bin/i386 -lR

Luca Chiappi
  • 289
  • 1
  • 3
  • 5

9 Answers9

23

OK. From previous experience I've found there are not many instructions on how to install rpy2 on Windows. I get that it's not officially supported but it's really not that hard. In a few steps I'll show what I did and maybe others can follow.

These instructions will most likely only work for Python 2.7+ and R 3.2+ but will probably work for R 3.0+. I am putting the paths that I've used on my system. Make sure to modify the paths accordingly:

First, make sure any previous rpy2 installations are UNINSTALLED before trying these steps (and obviously have R 3.2+ before installing).

  1. Download the .whl file for r2py from here
  2. Install the .whl file using pip install [put .whl filename here] (in the command line) from the directory where the file was downloaded
  3. Append a path similar to C:\Program Files\R\R-3.2.0\bin to your PATH environment variable
  4. Create a R_HOME system variable with a value similar to: C:\Program Files\R\R-3.2.0
  5. Create a R_USER system variable with your user name
  6. Create a R_LIBS_USER system variable with a path to the folder where external R packages are/will be installed.

That's it. Open up a command prompt and enter the command R. This should start an R session and display version information.

Now open up python and run this line to see if things worked out or not:

import rpy2.robjects as robjects 

Hopefully this helps.

pbreach
  • 16,049
  • 27
  • 82
  • 120
  • This works for Windows 10 with R-3.3.1 too! Rtools is necessary though. – Robin Kramer-ten Have Nov 30 '16 at 08:23
  • Can you please suggest which of the rpy2 binaries to download? I have R 3.3.2 and python 3.5.2. Thanks – splinter Feb 05 '17 at 19:28
  • 1
    @splinter There's 4 different binaries for python 3.5 (cp35), and two for 64-bit (amd64) and 32-bit (win32) systems. Narrowing down from there you can choose the latest rpy2 version (currently 2.8.5). – pbreach Feb 05 '17 at 19:46
  • Thank you @pbreach. Can you also please advise on which folder I should specify for the `R_LIBS_USER` system variable? I just installed R fresh. Should it be a new folder? Does it have to be somewhere in particular? – splinter Feb 05 '17 at 19:55
  • I'm not exactly sure, but you could try installing a package then look for where it might be. I don't have rpy2 installed anymore so I can't check – pbreach Feb 05 '17 at 20:19
  • I tried for a couple hours to install rpy2 by playing around with MSYS2, MinGW, environment variables, etc. Feels like when you can't get something to compile, it becomes a challenge to your intelligence (even though really its just your patience). Installing from that .whl went well. Thanks. – JoseOrtiz3 Dec 10 '18 at 00:36
15

I just successfully installed rpy2 on Windows 8.1 64bit with the binary installer from http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2. It is usually much easier to just download a binary instead of trying to compile a package in Windows.

Also, you should set R_HOME environmental variable (in my case, to C:\Program Files\R\R-3.1.1) before you install the rpy2 binary. Otherwise you get RuntimeError: R_HOME not defined. error when you try to import rpy2. (thanks to R_HOME Error with rpy2)

Lastly, make sure to use %load_ext rpy2.ipython instead of %load_ext rmagic if you want to use it in IPython. It is one of the new features in rpy2 version 2.4.0.

(thanks to IPython notebook and rmagic/rpy2: cannot find module ri2py (OSX 10.8.5, python 2.7, R 3.1))

Community
  • 1
  • 1
joon
  • 3,899
  • 1
  • 40
  • 53
  • Hi, Which version of python were you using and was it for 32 or 64 bits python? thanks, Luis – Luis Candanedo Jan 28 '16 at 20:29
  • Hi, Thanks, I managed to install it. However, it also affects how RStudio works. I get path errors. Did you experience the same? Thanks. Luis – Luis Candanedo Jan 30 '16 at 11:26
  • No - exactly what error are you getting with RStudio? I don't think any of this will affect how it works - maybe except `R_HOME`. Maybe you can generate a new question. – joon Feb 01 '16 at 02:03
  • 1
    This worked for me "out of the box", using rpy2-2.9.5-cp37-cp37m-win_amd64.whl on Python 3.7.4, Windows 10 Pro. – heisenBug Mar 02 '20 at 17:04
7

I also generally had no success getting Rpy2 to work on Windows. After a few hours hacking around with R2.15 and Rpy2 from the pre-compiled binary 2.3.4.win-amd64-py2.7, I followed this question, and installed pyper instead. Maybe this will work for you until the support is there. I for one would love to see Rpy2 on Windows and IPython notebook (%load_ext rmagic), but I just couldn't spend any more time with it.

Community
  • 1
  • 1
Marcus Jones
  • 960
  • 3
  • 11
  • 27
  • With the help of a professor, I was able to install rpy2 on a computer in my university (with Fedora as operating system) and now use that to work... I preferred rpy2 compared to pyper, because there is a lot of documentation about rpy2. – Luca Chiappi Mar 30 '13 at 21:47
  • Rpy2 is such a PITA to work with on Windows. Stay away from it. It is not supported properly. I finally gave up on rpy2 in Jupyter on Windows and went to Windows Docker Desktop running Ubuntu in order to use R cell magics. This avoids the (too) many unsolved problems with R on Windows. Why 3 to 5 different object-oriented systems within R? Different hacks at different times. The language needs to be rewritten so that it is more unified and consistent. R needs a benevolent dictator for life to guide it back to consistency. – Rich Lysakowski PhD Mar 14 '20 at 03:57
3

Following the answers above, this is what works for me

System: Win 7-64, Python 3.6,

1. Install R and packages you need

2. Set R_HOME and R_USER, as environment variable

R_HOME = D:\Program Files\R\R-3.4.1
R_USER = Administrator 

The user name can be obtained by echo %username% in cmd

3. Download rpy2 and install it

Download rpy2 from http://www.lfd.uci.edu/~gohlke/pythonlibs/

Check to its path, and install it in cmd like pip install rpy2-2.8.6-cp36-cp36m-win_amd64.whl

4. Create a notebook and see if it works

import rpy2.robjects as robjects

# test : evaluating R code
robjects.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)
        ''')
leerssej
  • 14,260
  • 6
  • 48
  • 57
ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
2

EDIT Finally I managed to get it to work!!! See below:

I have been banging my head to get this to work and still haven't figured it out completely. What I have found out is that the error

'sh' is not recognized as an internal or external command, operable program or batch file

is caused by the absence of mingw and Rtools (and other unix like helper programs). (http://sourceforge.net/p/rpy/mailman/message/26549197/)

I went passed this stage by installing Rtools (http://cran.r-project.org/bin/windows/Rtools/) and mingw (http://sourceforge.net/projects/mingw-w64/) (But since the mingw-w64 project on sourceforge.net is moving to mingw-w64.org i suggest to use mingw-w64.org)

I went passed the R.exe CMD config --ldflags error but got stuck with

TypeError: object of type 'NoneType' has no len()

error. I am still searching for a solution!!

Hope this helps someone at some point.

UPDATE : So it seems that there are couple of problems associated with the installation. The setup.py is not able to find the correct version of R and the compilers have issues.

So, the way to proceed :

  1. Download the source code from https://pypi.python.org/pypi/rpy2.
  2. Update your environment variables - In the user variables
    • PATH : append the path to ur R installation eg ;C:\Program Files\R\R-3.0.2\bin\x64
    • R_HOME : a new variable with path to R: C:\Program Files\R\R-3.0.2
    • R_USER : user user name eg john
  3. Install Rtools (http://cran.r-project.org/bin/windows/Rtools/) and mingw (http://mingw-w64.org)
  4. around line 123 before

    return self._version I added self._version = [3, 0] to force the version to be 3.0 (may be someone can solve this better)

  5. In the file unixccompiler.py either in python27/lib/distutils or in my case C:\Users(user_name)\AppData\Local\Continuum\Anaconda\Lib\distutils comment line 227

compiler = os.path.basename(sysconfig.get_config_var("CC")) with # (#compiler = os.path.basename(sysconfig.get_config_var("CC")))

and add line

compiler = 'gcc'

Some other things - use the command prompt cmd for installing. Use

python setup.py build install

Install the Rtools and mingw accepting all defaults. In Rtools select the

extra to build 64 bit R

and

edit the system PATH

Thats it!! Got it to work..

Hope this helps!!

POQDavid
  • 195
  • 1
  • 15
Atesh
  • 441
  • 4
  • 4
  • The "linux command not found" problems for *anything* are solved by using a Bash environment for windows (Git Bash). – JoseOrtiz3 Dec 09 '18 at 23:17
2

On Anaconda, you can install using:

conda install --channel https://conda.anaconda.org/Richarizardd rpy2

Alternativey, can be tried:

conda install -c r rpy2 

One can also refer:

https://github.com/conda-forge/rpy2-feedstock
Manoj Kumar
  • 5,273
  • 1
  • 26
  • 33
  • This returns the following message now:```PackageNotFoundError: Package not found: '' Dependency missing in current win-64 channels: - rpy2 -> r``` – leerssej Oct 18 '18 at 17:50
  • i think instead of down-voting, you should have corrected the solution with updated method.. when i wrote it in 2016, this was correct, but channel might have changedor something else in 2018...don't expect that things will not get updated... – Manoj Kumar Oct 20 '18 at 15:37
  • I totally agree. I didn't down vote. I just added a note for reference. I would have added an updated answer if I could have found a way to solve this issue. – leerssej Oct 20 '18 at 15:39
1

The install script setup.py is telling that a problem was met when running

"C:\PROGRA~1\R\R-215~1.2\bin\R" CMD config --ldflags

Open a terminal, paste that command, check what the error is.

As you edited the question with the output the problem seems to be that while running that command as a subprocess from python setup.py install an empty string is returned while when running the command from a terminal there is an output. I am also reading from the output from the terminal that cygwin is involved. May be this is the problem ?

lgautier
  • 11,363
  • 29
  • 42
  • Sometimes difficult to tell. The question is "What am I doing wrong ?". Telling how to identify what is wrong can be seen as an answer, albeit a partial one. – lgautier Feb 15 '13 at 07:31
  • 1
    I agree, but I feel that a comment is the appropriate way of getting an OP to give more information. – Paul Hiemstra Feb 15 '13 at 07:39
  • As they say, there many ways to skin a cat. Here the answer can be edited so the full steps from symptoms to a resolution of the problem can be nicely summarized in the end (rather than make it a treasure hunt through comments for the next person with the same issue) – lgautier Feb 15 '13 at 08:06
  • When executing `R.exe CMD config --ldflags` I get the error `'sh' is not recognized as an internal or external command, operable program or batch file.`, which leads me to believe R is assuming a *nix environment and trying to run a shell script. Running it from Cygwin bash doesn't help. – DavidJ Feb 27 '14 at 19:53
1

I am not sure if anyone is still following the discussion but in any case, My aim in trying to get rpy2 to work was so to be able to get it working in my Ipython notebooks. If anyone else is trying to do the same, I would suggest using IRkernel which installs smoothly as a cake (well made cake). Here are the resources to get it done. I have used it and it works like a charm.

basically what you need to run is this:

install.packages(c('rzmq','repr','IRkernel','IRdisplay'),repos = c('http://irkernel.github.io/', getOption('repos')))

IRkernel::installspec()

Here are the resources if you find any troubles:

https://github.com/IRkernel/IRkernel

http://blog.revolutionanalytics.com/2015/09/using-r-with-jupyter-notebooks.html

http://www.michaelpacer.com/maths/r-kernel-for-ipython-notebook

HTH

Atesh
  • 441
  • 4
  • 4
0

Check out this install doc I made about this issue. https://www.academia.edu/12178754/rpy2Install

It's directory specific to an ArcGIS setup, but quick to port

Jason Matney
  • 552
  • 6
  • 24