简体   繁体   English

将向量和参数从Python传递到R函数

[英]Passing vectors and params from Python to R functions

I am learning Python and R and am having an issue passing parameters from Python to an R function named "Contours". 我正在学习Python和R,并且在将参数从Python传递到名为“Contours”的R函数时遇到了问题。 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()
    }

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. 所以你可以说,我对R代码中的值的硬编码工作得很好,这是从Python到R的传递抛出了我。 I tried this.... 我试过这个....

R function R功能

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

and trying to pass from Python like this.... 并尝试像这样从Python传递....

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

and

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

Neither of which work. 两者都不奏效。 Has anyone encountered this before? 有没有人遇到过这个? Any pointers in the right direction would be greatly appreciated. 任何指向正确方向的人都会非常感激。 JW JW

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 ) 您可以导入R源,就像它是一个包(请参阅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")

Now to call the function Contours present in that namespace, you'll just use wrapper.Contours but you'll have to use Python syntax. 现在要调用该命名空间中存在的Contours函数,您只需使用wrapper.Contours但您必须使用Python语法。 In R scalars are vectors of length 1, but in Python scalars and sequences are quite different. 在R中,scalars是长度为1的向量,但在Python中,标量和序列是完全不同的。

If you want to use R's c() : 如果你想使用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)

Otherwise: 除此以外:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM