问题
I am trying to use r's density function through python, and I have to pass the 'from', 'to' arguments to the density functions. However, since the word 'from' is a reserved ketyword in python, how can I achieve this? Thank you. Here is the code so far.
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
回答1:
You can use dict
argument-unpacking to pass reserved words as parameter names:
r_a = r_density(f_a, bw='SJ', n=1024, **{'from':1, 'to':3})
or
r_a = r_density(f_a, **{'bw':'SJ', 'n':1024, 'from':1, 'to':3})
来源:https://stackoverflow.com/questions/41901917/rpy2-passing-python-reserved-keyword-arguments