如何设置环境变量 R_user 以在 python 中使用 rpy2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12698877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:37:50  来源:igfitidea点击:

How to setup environment variable R_user to use rpy2 in python

pythonrerror-handlinginstallation

提问by JPC

I 'm unable to run rpy2 in python.

我无法在 python 中运行 rpy2。

with this code

用这个代码

 import rpy2.robjects as robjects

Here's the full exceptions:

以下是完整的例外情况:



RuntimeError: R_USER not defined.

运行时错误:R_USER 未定义。

File "d:\py\r\r.python.py", line 1, in

文件“d:\py\r\r.python.py”,第 1 行,在

  import rpy2.robjects as robjects

File "c:\Python27\Lib\site-packages\rpy2\robjects\__init__.py", line 17, in <module>
  from rpy2.robjects.robject import RObjectMixin, RObject

File "c:\Python27\Lib\site-packages\rpy2\robjects\robject.py", line 5, in <module>
  rpy2.rinterface.initr()


I'm using window xp win32 Here're my locations:

我正在使用 windows xp win32 这是我的位置:

C:\Python27\Lib\site-packages\rpy2\robjects\robject.py

C:\Program Files\R\R-2.15.0\bin\i386\R.exe

C:\Python27\python.exe

回答by JPC

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

哦,nvm .. 我解决了这个问题 .. 这就是我是怎么做的,以防万一有人遇到同样的问题。我必须指定 PYTHONPATH 到 rpy2.robjects 存储的位置

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.

这应该工作,享受。

回答by lgautier

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

这可能是关于 bitbucket 的这个 rpy2 问题中讨论的内容。

回答by user3758274

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

这是我修复R包版本3.0.2python 版本2.7平台 ipython notebook 的方式。

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

更改 R 计算机的路径 -> 属性 -> 高级和系统设置 -> 环境变量

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

在用户变量字段中添加C:\Program Files\R\R-3.0.2\bin\x64(我的系统是 windows 64 位)到路径

In the system variable field add two new variables

在系统变量字段中添加两个新变量

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

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

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

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

回答by Lenka Vraná

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.

如果您想在 rpy2 中使用 Python,但又想继续使用 RStudio,请不要忘记将 RStudio 添加到您的路径中,否则您会遇到一些路径问题。

You can change your paths according to @user3758274:

您可以根据@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

更改 R 计算机的路径-> 属性-> 高级和系统设置-> 用户变量字段中的环境变量添加C:\Program Files\R\R-3.0.2\bin\x64(我的系统是 windows 64 位)到路径

在系统变量字段中添加两个新变量

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:

但是然后将 RStudio 添加到您的 R_USER 系统变量中,因此您将获得:

R_USER    C:\Program Files\RStudio\bin;C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2

回答by laven_qa

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

对于即时和临时解决方案,您可以在导入 rpy2 之前添加以下代码:

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

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

值得注意的一件事是您应该在路径中使用反斜杠而不是斜杠。

回答by Fran?ois M.

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

结合@laven_qa@user3758274 的答案,这对我有用

# 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]