pythonrrpy2wavelet

Introduce a Rlist in Python using rpy2


I am controlling R trough Python by rpy2 package. Everything works fine except when I have to introduce some function arguments throug a Rlist. Rlists are defined by the same keyword than Python: list; but its content it's very different. Since Python believes I am creating a Python list, instead of a Rlist, an error is always shown. I am using rpy2 to control WaveleComp R package.

Here I show an example in which I try to program legend_params:

import pandas as pd
# Import R manage tools:
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector, Matrix
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
R = ro.r
from rpy2.robjects import pandas2ri
pandas2ri.activate()
grdevices = importr('grDevices')    

data = pd.read_excel('Data.xlsx')      # Load data
wvt = importr('WaveletComp')           # Import WaveletComp R package
# Continuous Wavelet Transform of the data:
cwt = wvt.analyze_wavelet(data ,'Valores',loess_span = 0.75, dt=1,dj=1/20,
    lowerPeriod = 2, upperPeriod = 30, make_pval = True, n_sim = 100)


# Create image saver
grdevices.png(filename="Subsidencia_Wavelets.png", width = 14, height = 6,
              units = 'in', res = 1000, pointsize = 12)
# Save image (here is where error is generated)
wvt.wt_image(cwt, color_key = "quantile", main = (trayectoria + ' - ' + agregado),
    n_levels = 250, show_date = True, periodlab = "period (months)",
    legend_params = list(lab = "wavelet power levels", mar = 4.7))


# Close Graphical Device  
grdevices.graphics_off()

Out[]:

  File "C:/Users/Dell/wavelet.py", line 88, in <module>
    legend_params = list(lab = "wavelet power levels", mar = 4.7))

TypeError: list() takes no keyword arguments

I have tried creating a Rlist through rinterface, but a Nontype Object is created, what also shows an error.

Continue the previous example:

import rpy2.rinterface as ri
ri.initr()

lista = ri.initr('ListVector(lab = "wavelet power levels", mar = 4.7)')
wvt.wt_image(cwt, color_key = "quantile", main = (trayectoria + ' - ' + agregado),
    n_levels = 250, show_date = True, periodlab = "period (months)",
    legend_params = lista)


  File "C:\WPy64-3760\python-3.7.6.amd64\lib\site-packages\rpy2\robjects\conversion.py", line 60, in _py2ri
    raise NotImplementedError("Conversion 'py2ri' not defined for objects of type '%s'" % str(type(obj)))

NotImplementedError: Conversion 'py2ri' not defined for objects of type '<class 'NoneType'>'

I'd be really greatful with someone who could help me. Thank you very much.


Solution

  • rpy2.rinterface.initr is used to initialize the embedded R, and you will not need to worry about it if using the rpy2.robjects interface.

    ListVector is an rpy2 class, only visible from the Python side: https://rpy2.github.io/doc/v3.3.x/html/vector.html#rpy2.robjects.vectors.ListVector

    The constructor for ListVector works like this:

    lista = rpy2.robjects.ListVector({'lab': "wavelet power levels", 'mar': 4.7})