UsageError: unrecognized arguments: in %R line

3.3k views Asked by At

Since I discovered rpy2 and the possibility to use %R within my ipython notebook, my coding has become much easier. But I might have hit a wall.

I need to generate value from a stable distribution. I am using stabledist package from R.

I need to run the command:

      Fx = pstable(seq(-2,4,0.1), alpha =alfa_x, beta = -1, gamma = scale_x, delta = delta_x, pm = 1, lower.tail = TRUE, log.p = FALSE, subdivisions = 1000)

when I define one of my cell as a R cell, and I run the command there:

      %%R
      Fx = pstable(.....

everything works great.

but I need to place this function within a python script. So far I have used many R packages, and the push/pull of data has worked perfectly, so it has the use of lines of R codes (with %R rmagic) within python scripts.

however with this one, if I call the same package and function within a python script, in the following way:

      python code...
      %Rpush alfa_x
      %Rpush scale_x
      %Rpush delta_x
      %R Fx = pstable(seq(-2,4,0.1), alpha =alfa_x, beta = -1, gamma = scale_x, delta = delta_x, pm = 1, lower.tail = TRUE, log.p = FALSE, subdivisions = 1000)

I get the Usage error:

     UsageError: unrecognized arguments:.....

I basically get some of the error reported in this old [thread][1]

Any suggestion?

(I did try to use %%R within my python code, but it doesn't change anything)

[1]https://bitbucket.org/rpy2/rpy2/issue/253/r-select-flights-year-day

3

There are 3 answers

2
agstudy On BEST ANSWER

One option is to to use the classic way :

import rpy2.robjects as robjects
FX= robjects.r('''
         pstable(seq(-2,4,0.1), 
                     alpha =alfa_x, 
                     beta = -1, 
                     gamma = scale_x, 
                     delta = delta_x, 
                     pm = 1, lower.tail = TRUE, 
                     log.p = FALSE, subdivisions = 1000)
           ''')
0
claude On

I found a workaround to this issue. But I would like to know if there is a proper solution to this.

I created, an R cell, a "workaround" function:

%%R
library(stabledist)
workaround_pstable <- function(x,alfa_x,scale_x,delta_x)
{F = pstable(x, alpha =alfa_x, beta = -1, gamma = scale_x, delta = delta_x, pm = 1, lower.tail = TRUE, log.p = FALSE, subdivisions = 1000)
 return(F)}

and then within my python code in the ipython cell, I call the workaround function

 python code...
 %Rpush alfa_x
 %Rpush scale_x
 %Rpush delta_x
 %R Fx = workaround_pstable(Id_unique,alfa_x,scale_x,  delta_x)

it worked.

1
lgautier On

You might achieve better code clarity by just calling the R function as exposed by rpy2:

from rpy2 import robjects
# get whatever is called "pstable", just like when writing
# "pstable" in the R console
pstable = robjects.globalenv.get('pstable')
# same for seq... but since I know that it is coming from base, I
# get it from there
seq = robjects.baseenv['seq']
Fx = pstable(seq(-2,4,0.1),
             alpha = alfa_x,
             beta = -1,
             gamma = scale_x,
             delta = delta_x,
             pm = 1,
             lower_tail = True,
             log_p = False,
             subdivisions = 1000)