rpy2: How to make ListVector with more than 1 elements?

25 Views Asked by At

I'm using the rpy2 package in python to create a list within a list and then pass it back through R.

I am able to successfully get it to produce a list with 1 element but now I want it to produce a list with more than 1 element, such that when I call my list object in python it says: ListVector with 2 elements.

Here is the code I am working off:

import rpy2.robjects as ro

JobIDs = [2201795, 2201783]
r_list = ["a", "b", "c"]
nested_r_lists = []  # Initialize an empty list outside the loop
counter = 0
for i in JobIDs:
    print(i)
    # Create a new ListVector for each job id
    list_vector = ro.ListVector({'Excel_List' + str(i): r_list})
    # Append the ListVector to nested_r_lists
    nested_r_lists.extend([list_vector])

# Now nested_r_lists contains two elements, one for each job id
nested_r_lists

Inside: nested_r_lists is says this:

[<rpy2.robjects.vectors.ListVector object at 0x000001DE120810D0> [19]
 R classes: ('list',)
 [ListSexpVector]
   Excel_List2201795: <class 'rpy2.rinterface.ListSexpVector'>
   <rpy2.rinterface.ListSexpVector object at 0x000001DE11A26650> [19],
 <rpy2.robjects.vectors.ListVector object at 0x000001DE121D1410> [19]
 R classes: ('list',)
 [ListSexpVector]
   Excel_List2201783: <class 'rpy2.rinterface.ListSexpVector'>
   <rpy2.rinterface.ListSexpVector object at 0x000001DE12128750> [19]]

But when I call nested_r_lists[0] and nested_r_lists[1] it reports

ListVector with 1 elements.
Excel_List2201795   [19]
ListVector with 1 elements.
Excel_List2201783   [19]

Instead, I would like to call nested_r_lists and have the output be:

ListVector with 2 elements.
Excel_List2201795   [19]
Excel_List2201783   [19]

Please help! Thank you!

0

There are 0 best solutions below