Search code examples
pythonrrpy2

rpy2: how to get the return values from calling a R function


I would like to use the following R script in Python:

> library(bfast)
> apple <- read.csv("/Users/nskalis/Downloads/R/apple.csv", sep = ";", header=TRUE)
> data = apple
# data$in_bps: is vector of double numbers
> data.ts <- ts(data$in_bps, frequency=1)
> data.fit <- bfast(data.ts, h=0.1, season="none", max.iter=1)
> data.fit$output[[1]]$Tt
> data.fit$output[[1]]$Vt.bp
> data.fit$output[[1]]$ci.Vt
> data.fit$output[[1]]$ci.Vt$confint

Therefore I am using rpy2 and I have done the following:

from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
importr("bfast")
data = range(1,100)
data = robjects.FloatVector(data)
data = robjects.r.ts(data, frequency=1)
x = robjects.r.bfast(data, h=0.1, season="none", max_iter=1)

The result variable x equals

In [42]: x
Out[42]: 
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8 / R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
  Yt: <class 'rpy2.robjects.vectors.FloatVector'>
  R object with classes: ('ts',) mapped to:
<FloatVector - Python:0x7f234fd22dc8 / R:0x7605740>
[1.000000, 2.000000, 3.000000, ..., 97.000000, 98.000000, 99.000000]
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8 / R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8 / R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
  ...
  Yt: <class 'rpy2.robjects.vectors.FloatVector'>
  R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x7f234c053388 / R:0x586b668>
[0.000000]
  output: <class 'rpy2.robjects.vectors.BoolVector'>
  R object with classes: ('logical',) mapped to:
<BoolVector - Python:0x7f234c04eac8 / R:0x57ee518>
[NA]
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8 / R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]

Could you please advise how to get the variable data.fit$output[[1]]$Vt.bp ?

PS: it is my 1st time with rpy2, so please feel free to advise me if I am doing anything wrong already.


Solution

  • As seen, the bfast method apparently returns a nested object with layers deep of data items. Additionally, the returned Python object is of a foreign class <class 'rpy2.robjects. vectors.ListVector'> with nested, unnamed elements.

    As an alternative to mining the Python object by integer position, consider importing a user-defined R function using the anonymous package import facility, STAP, that allows you to retain the R code and specifically return the variable you require using R's named elements nomenclature:

    from rpy2.robjects.packages import STAP
    
    r_fct_string ='''   
    bfast_out <- function(path){
        data <- read.csv(path, sep = ";", header=TRUE)
    
        data.ts <- ts(data$in_bps, frequency=1)
        data.fit <- bfast(data.ts, h=0.1, season="none", max.iter=1)
    
        data.fit$output[[1]]$Vt.bp    
    }
    '''
    
    r_pkg = STAP(r_fct_string, "r_pkg")
    
    Vt_bp = r_pkg.bfast_out("/Users/nskalis/Downloads/R/apple.csv")
    
    print(Vt_bp)