使用 rpy2 在 Python 中引入一个 Rlist
Introduce a Rlist in Python using rpy2
我通过 rpy2 包 Python 控制 R 槽。
一切正常,除非我必须通过 Rlist 引入一些函数参数。
Rlists 由与 Python 相同的关键字定义:list
;但它的内容却大不相同。
因为 Python 认为我正在创建一个 Python 列表,而不是 Rlist,所以总是显示错误。
我正在使用 rpy2 来控制 WaveleComp R 包。
这里我展示了一个我尝试编程的例子 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
我已经尝试通过 rinterface
创建一个 Rlist,但是创建了一个非类型对象,这也显示了一个错误。
继续前面的例子:
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'>'
如果有人能帮助我,我会非常感激。非常感谢。
rpy2.rinterface.initr
用于初始化嵌入式R,如果使用rpy2.robjects
接口就不用担心了。
ListVector
是一个 rpy2 class,仅从 Python 一侧可见:
https://rpy2.github.io/doc/v3.3.x/html/vector.html#rpy2.robjects.vectors.ListVector
ListVector
的构造函数是这样工作的:
lista = rpy2.robjects.ListVector({'lab': "wavelet power levels", 'mar': 4.7})
我通过 rpy2 包 Python 控制 R 槽。
一切正常,除非我必须通过 Rlist 引入一些函数参数。
Rlists 由与 Python 相同的关键字定义:list
;但它的内容却大不相同。
因为 Python 认为我正在创建一个 Python 列表,而不是 Rlist,所以总是显示错误。
我正在使用 rpy2 来控制 WaveleComp R 包。
这里我展示了一个我尝试编程的例子 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
我已经尝试通过 rinterface
创建一个 Rlist,但是创建了一个非类型对象,这也显示了一个错误。
继续前面的例子:
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'>'
如果有人能帮助我,我会非常感激。非常感谢。
rpy2.rinterface.initr
用于初始化嵌入式R,如果使用rpy2.robjects
接口就不用担心了。
ListVector
是一个 rpy2 class,仅从 Python 一侧可见:
https://rpy2.github.io/doc/v3.3.x/html/vector.html#rpy2.robjects.vectors.ListVector
ListVector
的构造函数是这样工作的:
lista = rpy2.robjects.ListVector({'lab': "wavelet power levels", 'mar': 4.7})