rpy2 传递 python 保留关键字参数

rpy2 passing python reserved keyword arguments

我正在尝试通过 python 使用 r 的密度函数,我必须将 'from'、'to' 参数传递给密度函数。但是,由于单词 'from' 是 python 中的保留关键字,我该如何实现呢? 谢谢你。 这是到目前为止的代码。

r_density=robjects.r('density')
f_a = robject.FloatVector(a)
r_a = r_density(f_a, bw='SJ', n=1024) ## Here I need to add 'from' and 'to' arguments

您可以使用 dict argument-unpacking 将保留字作为参数名称传递:

r_a = r_density(f_a, bw='SJ', n=1024, **{'from':1, 'to':3}) 

r_a = r_density(f_a, **{'bw':'SJ', 'n':1024, 'from':1, 'to':3})