Combining R and python through rpy2: How to read in a python list to R

413 Views Asked by At

I have found similar questions here and sort of here, but I can't seem to figure out how to do it for my own data.

I have a set of lists of floats in python (in reality, each list is about 1,000 floats long); e.g.

[0.01,0.02,0.03,0.04,0.05]
[0.1,0.2,0.4,0.5,0.6,0.7]
[0.01,0.2,0.05,0.4]

For each list, I want to convert the python list to an R list, perform a FDR test on the R list to get a list of Q values, and then convert the R list of Q values back to a python list and then continue on with my script.

The code I have:

for each_list in SetOfLists:
    ro.r("library('devtools')") #load necessary package for task 
    ro.r("library('qvalue')")   #load necessary package for task
    pvals = ro.FloatVector(each_list)  #explain that each list is a set of floats 
    print ro.r("qobj <-qvalue(p=" + pvals + ")") #run the r function on each list
    #ro.r("qobj$lfdr") #get the FDR values from the R output                                                                                                                                                   
    #Then convert this list of FDR values back to python  

I'm having a problem with this line:

print ro.r("qobj <-qvalue(p=" + pvals + ")")

For example, if I make that line:

  print ro.r("qobj <-qvalue(p=" + pvals + ")")

The error is:

> Traceback (most recent call last):   File "CalculateFDR.py", line 33,
> in <module>
>     print ro.r("qobj <-qvalue(p=" + pvals + ")") TypeError: cannot concatenate 'str' and 'FloatVector' objects

If I change the line slightly to:

print ro.r("qobj <-qvalue(p= pvals)")

The error is:

  res = super(Function, self).__call__(*new_args, **new_kwargs)
Traceback (most recent call last):
  File "CalculateFDR.py", line 33, in <module>
    print ro.r("qobj <-qvalue(p=pvals)")
  File "/home/nis/aoife/env/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py", line 321, in __call__
    res = self.eval(p)
  File "/home/nis/aoife/env/local/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 178, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/home/nis/aoife/env/local/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 106, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in qvalue(p = pvals) : object 'pvals' not found

I know the problem is I'm not properly converting the python list to an R list, I'm not sure how to do this correctly; hence advice is appreciated.

1

There are 1 best solutions below

0
On

Just in case this helps anyone else, the answer is:

devtools = importr("devtools")
qvalue = importr("qvalue")

for each_list in SetOfLists:
    pvals = ro.FloatVector(v)
    rcode = 'qobj <-qvalue(p=%s)' %(pvals.r_repr())
    res = ro.r(rcode)
    r_output1 = 'qobj$pvalue'
    r_output2 = 'qobj$qvalue'
    r_pvalue = ro.r(r_output1)
    r_qvalue = ro.r(r_output2)
    DictOfValues = dict(zip(r_pvalue,r_qvalue))

This is the code to get a python list into R.