简体   繁体   English

使用 rpy2 调用 R 包函数

[英]Calling an R package function with rpy2

I'm new to R and need to pass string data from a pandas dataframe to a function in R. This function accepts nested lists of strings, such as:我是 R 新手,需要将字符串数据从 pandas 数据帧传递到 R 中的函数。此函数接受嵌套的字符串列表,例如:

> list(c("HP:0001315", "HP:0011343"), c("HP:0007164", "HP:0030810"), c( "HP:0030133", "HP:0040082"))
[[1]]
[1] "HP:0001315" "HP:0011343"

[[2]]
[1] "HP:0007164" "HP:0030810"

[[3]]
[1] "HP:0030133" "HP:0040082"  

The code编码

# Importing the package `ontologySimilarity`:

from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
utils = importr('utils')
utils.data('hpo')
ontology_similarity = importr('ontologySimilarity')
   

I tried two approaches:我尝试了两种方法:

1) 1)

lists = numbers_and_ids.HPO_ID.str.split('\t')
ontology_similarity.get_sim_grid(ontology='hpo', term_sets=lists)

That yielded KeyError: <class 'list'> error message.这产生了KeyError: <class 'list'>错误消息。

2) 2)

# numbers_and_ids dataframe from which the data will be taken:

data = {'dna_#': {0: '25246', 1: '29244', 2: '6409'},
        'HPO_ID': {0: 'HP:0001263\tHP:0001508\tHP:0000252\tHP:0001875\tHP:0001627', 1: 'HP:0011344\tHP:0008936\tHP:0001257\tHP:0005305\tHP:0002188\tHP:0040187\tHP:0000365\tHP:0001999', 2: 'HP:0001263\tHP:0000252\tHP:0001629\tHP:0001875\tHP:0001999'}}

numbers_and_ids = pandas.DataFrame(data}

numbers_and_ids
Out[89]:19:00
   dna_#                                             HPO_ID
0  25246  HP:0001263\tHP:0001508\tHP:0000252\tHP:0001875...
1  29244  HP:0011344\tHP:0008936\tHP:0001257\tHP:0005305...
2   6409  HP:0001263\tHP:0000252\tHP:0001629\tHP:0001875...

# Converting the data in the dataframe into tuples:

_1 = tuple(numbers_and_ids.HPO_ID.str.split('\t')[0])
_2 = tuple(numbers_and_ids.HPO_ID.str.split('\t')[1])
_3 = tuple(numbers_and_ids.HPO_ID.str.split('\t')[2])

# Creating the nested list d and calling the function:

robjects.r(f'd<-list(c{_1}, c{_2}, c{_3})')
ontology_similarity.get_sim_grid(ontology='hpo', term_sets='d')

That yielded the error message:这产生了错误消息:

rpy2.rinterface_lib.embedded.RRuntimeError: Error in (function (ontology, information_content, term_sim_method, term_sim_mat,  : 
  is.list(term_sets) & is.list(term_sets) is not TRUE

I checked if d is a list or not:我检查了 d 是否是一个列表:

robjects.r(f'print(is.list(d))')

[1] TRUE
Out[92]: 
<rpy2.robjects.vectors.BoolVector object at 0x7f591524a900> [RTYPES.LGLSXP]
R classes: ('logical',)
[       1]

I'd be grateful to get any suggestion regarding how to call ontology_similarity.get_sim_grid() .我会很高兴得到有关如何调用ontology_similarity.get_sim_grid()的任何建议。
Thanks.谢谢。

最终,我使用robjects.r从 R 而不是 Python 调用 R 包,并且它起作用了。

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

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