【问题标题】:How can i connect pyRserve with Python我如何将 pyRserve 与 Python 连接起来
【发布时间】:2016-09-08 19:51:33
【问题描述】:

我需要在 iPython 中连接 pyRserve,但尝试连接时出现错误。这是错误。

conn = pyRserve.connect()

这就是我得到的:

RConnectionRefused: Connection denied, server not reachable or not accepting connections.

在 pyrserve 手册中有纠正此问题的建议,但我不明白我需要做什么。这是建议(注)

注意当应该打开到 Rserve 的远程连接时,pyRserve 无法连接到它,很可能 Rserve 只监听它自己的内部网络连接。要强制 Rserve 接受来自其他机器的连接,请创建一个名为 /etc/Rserv.conf 的文件并至少添加以下行:remote enable 然后重启 Rserve。

所以,我需要知道如何实现note并在python中连接Rserve

谢谢大家

【问题讨论】:

    标签: python r rserve pyrserve


    【解决方案1】:

    在 R 中(使用 3.3.2)

    install.packages(‘Rserve’)
    library(Rserve)
    Rserve()    # the actual server you will be calling from python
    

    运行 Rserve() 后,您应该看到以下指示 R 服务器正在运行: 开始储备... "C:\Users\bkeith\Desktop\R-33~1.2\library\Rserve\libs\x64\Rserve.exe"


    在 Python 中

    import pyReserve
    conn = pyRserve.connect()  # the connection to R
    
    print conn
    >> <Handle to Rserve on localhost:####>
    

    打印后,您就可以开始使用该库了。


    不要忘记关闭你的连接,你可以使用 conn.isClosed 来检查它:

    conn.shutdown()
    

    以下是库中的一些示例:

    Ex.1 – 基本语法

    conn.eval(‘2+4’) #basic eval
    >> 6.0
    

    Ex.2 – 从 python 到 R 的列表

    a = [1,2,3,4]   #python list
    conn.r.a = a    #the R variable is named a as well 
    print conn.r.a 
    >>[1,2,3,4]     #the R list is now in python as well
    

    Ex.3 – R 中的函数

    conn.voidEval(‘two <- function(x){ x * 2}’) #declare the function
    conn.eval(‘two(9)’)   #use the function
    >> 18.0
    

    【讨论】:

      【解决方案2】:

      我之前也遇到过同样的问题,通过安装Rserve 解决了这个问题,请确保从他们的网站下载文件并运行以下代码:

      R CMD INSTALL [Rserve_1.8-0.tar.gz] #put in the file name
      

      然后在终端运行时:

      R CMD Rserve
      

      应该有消息表明r处于守护模式,然后从那里运行conn = pyRserve.connect()应该没有问题。

      【讨论】: