Using rpy2 to access R packages in Python

Viewed 201

Gurus / Fellow enthusiasts,

I am using rpy2 to access R functions in python. And having problems with calling a function called 'seas' which resides in R's "seasonal" package. (via jupyter notebook on windows 10)

ts_r   = robjects.r('ts')
input_5 = ts_r(series.values, frequency=12, start=2001)  
is_ts_r(input_5)          # get TRUE for this function which checks for 'ts' object 
seas_r  = robjects.r('seas')
model = seas_r(input_5)   # this line gives **ERROR**

In order to test the correct package installation I did the following test and it was successful.

output_r = robjects.r('seas(AirPassengers)') # can view output for air passenger data (pre-defined in R)

I did another test to check for the 'ts' object generated in python. I called another function called 'stl' in R's "stats" package thats uses 'ts' object and it works.

stl_r = robjects.r('stl')
output_model_stl = stl_r(input_5, "periodic")  # ** THIS WORKS**

I have included the error below for better readability. Would appreciate any suggestions for debugging the problem.

    model = seas_r(input_5)

R[write to console]: Error in align_x_list(x, list) : 
  all(sapply(x, inherits, "ts")) is not TRUE

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_25148/2372710267.py in <module>
----> 1 model = seas_r(input_5)

c:\python39\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    196                 v = kwargs.pop(k)
    197                 kwargs[r_k] = v
--> 198         return (super(SignatureTranslatedFunction, self)
    199                 .__call__(*args, **kwargs))
    200 

c:\python39\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    123             else:
    124                 new_kwargs[k] = conversion.py2rpy(v)
--> 125         res = super(Function, self).__call__(*new_args, **new_kwargs)
    126         res = conversion.rpy2py(res)
    127         return res

c:\python39\lib\site-packages\rpy2\rinterface_lib\conversion.py in _(*args, **kwargs)
     43 def _cdata_res_to_rinterface(function):
     44     def _(*args, **kwargs):
---> 45         cdata = function(*args, **kwargs)
     46         # TODO: test cdata is of the expected CType
     47         return _cdata_to_rinterface(cdata)

c:\python39\lib\site-packages\rpy2\rinterface.py in __call__(self, *args, **kwargs)
    678             )
    679             if error_occured[0]:
--> 680                 raise embedded.RRuntimeError(_rinterface._geterrmessage())
    681         return res
    682 

RRuntimeError: Error in align_x_list(x, list) : 
  all(sapply(x, inherits, "ts")) is not TRUE
0 Answers
Related