How to setup environment variable R_user to use rpy2 in python

前端 未结 6 1165
暗喜
暗喜 2020-11-28 08:43

I \'m unable to run rpy2 in python.

with this code

 import rpy2.robjects as robjects

Here\'s the full exceptions:


R

相关标签:
6条回答
  • 2020-11-28 09:19

    If you want to use Python with rpy2 but you also want to keep using your RStudio, don't forget to add RStudio to your path as well, or you'll get some path issues.

    You can change your paths according to @user3758274:

    Change Path for R computer-> property -> advanced and system setting -> environment variables in the user variable field add C:\Program Files\R\R-3.0.2\bin\x64 (my system is windows 64bit) to path

    In the system variable field add two new variables

    R_HOME    c:\program files\r\r-3.0.2
    
    R_USER    C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2
    

    But then add also RStudio to your R_USER system variable, so you'll get:

    R_USER    C:\Program Files\RStudio\bin;C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2
    
    0 讨论(0)
  • 2020-11-28 09:22

    Combining answers from @laven_qa and @user3758274, here is what worked for me :

    # installing steps after downloading .whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2
    import pip
    pip.main(["install", "C:/Users/YOUR_USERNAME/Downloads/rpy2-2.8.6-cp36-cp36m-win_amd64.whl"]) # Path to the file that was downloaded from the website above
    
    # setting temporary PATH variables
    import os
    os.environ['R_HOME'] = 'C:\Program Files\Microsoft\R Open\R-3.4.0' #path to your R installation
    os.environ['R_USER'] = 'C:\ProgramData\Anaconda3\Lib\site-packages\rpy2' #path depends on where you installed Python. Mine is the Anaconda distribution
    
    # importing rpy2
    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)
            ''')
    
    # returns : 
    > R object with classes: ('numeric',) mapped to:
    > <FloatVector - Python:0x000000000C260508 / R:0x000000000F2872E8>
    > [18.849556]
    
    0 讨论(0)
  • 2020-11-28 09:24

    OH, nvm .. I fixed this .. here's how i did it , just incase anyone have the same issue. I have to specify PYTHONPATH to location rpy2.robjects stored

    Here's in details : My Computer > System properties > Advanced > Environment Variables :

    Under system variables create or edit your

    Variable name : PYTHONPATH 
    
    Variable value : C:\Python27\Lib\site-packages\rpy2;C:\Program Files\R\R-2.15.0\bin\i386;C:\Python27\Lib\site-packages\rpy2\robjects
    

    This should work, enjoy.

    0 讨论(0)
  • 2020-11-28 09:27

    Here is the way I fixed my R package version 3.0.2 python version 2.7 platform ipython notebook.

    Change Path for R computer-> property -> advanced and system setting -> environment variables

    in the user variable field add C:\Program Files\R\R-3.0.2\bin\x64 (my system is windows 64bit) to path

    In the system variable field add two new variables

    R_HOME c:\program files\r\r-3.0.2

    R_USER C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2

    0 讨论(0)
  • 2020-11-28 09:36

    This might be what is discussed in this rpy2 issue on bitbucket.

    0 讨论(0)
  • 2020-11-28 09:37

    For an instant and temporary solution, you can add the following code before importing rpy2:

    import os
    os.environ['R_HOME'] = 'C:/program files/R-3.3.1'
    

    One thing worth noting is that you should use backslash instead of slash in the path.

    0 讨论(0)
提交回复
热议问题