Passing vectors and params from Python to R functions

Jane WIlkie

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)

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

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

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. I tried this....

R function

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

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)

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

lgautier

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")

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.

If you want to use R's 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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R tidyeval passing a list containing multiple character vectors to dplyr functions

Having trouble figuring out params, arguments, passing variables to functions in Python

ReactJS passing params to exported functions

Passing Python functions from YAML file

Passing params from views to controller

Passing and returning functions in R

passing matrices in functions in python

Rewriting functions from r to python

AngularJS : passing params from controller to service

Angular: Passing params from $routeProvider to controller

Passing multiple params from createAsyncThunk to my page

Passing params from alsa application to kernel driver

Passing params from one Axios request to another

Passing multiple arguments from python to R

Calling a python function from R with passing the arguments

python group by, passing in columns to aggregate function params

Dynamically passing the object properties as params of a method in Python

Python requests module not passing params in session

Is it possible to call functions with different amounts of params, passing the same parameter pack

How to source functions but not vectors in R script

R functions: passing arguments with ellipsis

Passing in unbound variables into R functions

Passing a list of functions to ddply in R

Practising on creating functions and passing params | Getting null when trying to use params on querySelectors

R Combining list from vectors

Python decorators vs passing functions

Passing default arguments to functions in Python

Python: Passing variables between functions

Python 3: Passing functions into *args