将向量和参数从 Python 传递到 R 函数 [英] Passing vectors and params from Python to R functions

查看:100
本文介绍了将向量和参数从 Python 传递到 R 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python 和 R,但在将参数从 Python 传递到名为Contours"的 R 函数时遇到了问题.下面的作品.....

I am learning Python and R and am having an issue passing parameters from Python to an R function named "Contours". The below works.....

Python (testr.py)

Python (testr.py)

import rpy2.robjects as robjects
robjects.r('''
       source('Wrapper.R')
''')

r_myfunc = robjects.globalenv['Contours']
r_myfunc()

R (Wrapper.R)

R (Wrapper.R)

source ('./DrawCompRecVark.R')
imageDirectory="./tmp/"
    Contours <- function()
    {
      k=c(0,1)
      sens=c(0.8, 0.9, 0.95, 0.995)
      spec=0.8
      prev=0.001
      N=1
      uniqueId=1
      #graph
      prepareSaveGraph(imageDirectory, "PPVkSensSpec-", uniqueId)
      DrawSensSpec(k, sens, spec, prev, N)
      dev.off()

      #save the cNPV graph
      prepareSaveGraph(imageDirectory, "cNPVkSensSpec-", uniqueId)
      DrawVarcSpec(k, sens, spec, prev, N)
      dev.off()
    }

如您所知,我对 R 代码中值的硬编码工作正常,是从 Python 到 R 的传递让我感到困惑.我试过了....

So as you can tell, my hard coding of the values in the R code works fine, it's the passing from Python to R that throws me. I tried this....

R 函数

Contours <- function(k, sens, spec, prev, N, uniqueId)

并尝试像这样从 Python 传递....

and trying to pass from Python like this....

r_myfunc( c(0,1), c(0.8, 0.9, 0.95, 0.995), 0.8, 0.001, 1, 986)

r_myfunc( "c(0,1)", "c(0.8, 0.9, 0.95, 0.995)", 0.8, 0.001, 1, 986)

两者都不起作用.有没有人遇到过这个?任何指向正确方向的指针将不胜感激.JW

Neither of which work. Has anyone encountered this before? Any pointers in the right direction would be greatly appreciated. JW

推荐答案

您可以像导入包一样导入 R 源(请参阅 http://rpy.sourceforge.net/rpy2/doc-2.5/html/robjects_rpackages.html#importing-arbitrary-r-code-as-a-package)

You can import an R source as if it was a package (see http://rpy.sourceforge.net/rpy2/doc-2.5/html/robjects_rpackages.html#importing-arbitrary-r-code-as-a-package)

import os
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage

with open('Wrapper.R') as fh:
    rcode = os.linesep.join(fh.readlines())
    wrapper = SignatureTranslatedAnonymousPackage(rcode, "wrapper")

现在要调用该命名空间中存在的函数 Contours,您只需使用 wrapper.Contours,但您必须使用 Python 语法.在 R 中标量是长度为 1 的向量,但在 Python 中标量和序列完全不同.

Now to call the function Contours present in that namespace, you'll just use wrapper.Contours but you'll have to use Python syntax. In R scalars are vectors of length 1, but in Python scalars and sequences are quite different.

如果你想使用R的c():

from rpy2.robjects.packages import importr
base = importr("base")
wrapper.Contours(base.c(0,1),
                 base.c(0.8, 0.9, 0.95, 0.995),
                 0.8, 0.001, 1, 986)

否则:

from rpy2.robjects.vectors import IntVector, FloatVector
wrapper.Contours(IntVector((0,1)),
                 FloatVector((0.8, 0.9, 0.95, 0.995)),
                 0.8, 0.001, 1, 986)

这篇关于将向量和参数从 Python 传递到 R 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆