Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rpy2: how to access the R list-type variable?

Tags:

python

r

rpy2

I can fit a model in Jupyter/Python using rpy2, however, the result returned is a list type value in R. For example

# Cell #1, load rpy2 and re
%load_ext rpy2.ipython
%R require(ggplot2)
%R require(movMF)

# Cell #2, generate data from Python
from scipy.stats import vonmises
kappa = 5
samples = vonmises.rvs(kappa, size=100)
data = [cos(samples), sin(samples)]

# Cell #3, fit model using R and rpy2
%%R -i data -o result
result = movMF(data, 1, nruns = 10)

# Cell #4, print result
print(result)

The result looks like this:

enter image description here

If I type result it returns

R object with classes: ('movMF',) mapped to:
<ListVector - Python:0x00000000103B4448 / R:0x0000000010CB2380>
[Matrix, Float..., Float..., ..., IntVe..., Float..., ListV...]
  theta: <class 'rpy2.robjects.vectors.Matrix'>
  R object with classes: ('matrix',) mapped to:
<Matrix - Python:0x0000000004FCF388 / R:0x0000000010C5B2B8>
[5.235426, -0.023640]
  alpha: <class 'rpy2.robjects.vectors.FloatVector'>
  R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x00000000103B4588 / R:0x0000000011F6B6B0>
[1.000000]
  L: <class 'rpy2.robjects.vectors.FloatVector'>
  R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x00000000103B4BC8 / R:0x00000000114FB6F0>
[118.877731]
  ...
  theta: <class 'rpy2.robjects.vectors.IntVector'>
  R object with classes: ('integer',) mapped to:
<IntVector - Python:0x0000000011441248 / R:0x0000000011F6B980>
[       1]
  alpha: <class 'rpy2.robjects.vectors.FloatVector'>
  R object with classes: ('logLik',) mapped to:
<FloatVector - Python:0x00000000114415C8 / R:0x000000000EE447E0>
[118.877731]
R object with classes: ('movMF',) mapped to:
<ListVector - Python:0x00000000103B4448 / R:0x0000000010CB2380>
[Matrix, Float..., Float..., ..., IntVe..., Float..., ListV...]

I want to know, how can I access to its inner values?

So far I can only play with result[0], not sure if this is the right way.


In R environment, this is the data structure:

enter image description here

and I can access the value like result$theta

like image 888
cqcn1991 Avatar asked Aug 04 '17 02:08

cqcn1991


2 Answers

print(pamk_clusters$pamobject$clusinfo)

will not work, and its equivalent

print(pamk_clusters[["pamobject"]][["clusinfo"]])

also will not work ... however, after some digging in the "man"

https://rpy2.github.io/doc/latest/html/vector.html#extracting-r-style

Access to R-style extracting/subsetting is granted though the two delegators rx and rx2, representing the R functions [ and [[ respectively.

This works as expected

print(pamk_clusters.rx2("pamobject").rx2("clusinfo"))

I commented in the forums about "man" clarity:

https://bitbucket.org/rpy2/rpy2/issues/436/acessing-dataframe-elements-using-rpy2

like image 50
mshaffer Avatar answered Oct 03 '22 03:10

mshaffer


I found two ways of doing this. Suppose your list is:

a_list <- list(x1 = c(1,2,3), x2 = c(4,5,6))

You can access a_list$x1 via rpy2 by doing:

print(a_list.rx2('x1'))

or ( https://rpy2.github.io/doc/latest/html/vector.html#assigning-r-style ):

print(a_list[a_list.names.index('x1')])
like image 42
pietrop Avatar answered Oct 03 '22 02:10

pietrop