If you want to use R functions in ipython, use %% R or
python
import pyper as pr
r = pr.R(use_pandas =True)
With this, you don't care about variable scope more than %% R
python
def sample(lambda_target, sample_size):
return r('rpois(%d, %f)'%(sample_size, lambda_target))
It is convenient to make such a python method
But with this, the return value is
'try({rpois(50, 8.000000)})\n [1] 8 6 11 8 10 9 8 10 5 11 8 4 13 11 4 10 5 14 5 9 11 10 7 9 1\n[26] 8 6 10 4 8 14 13 3 8 13 2 9 6 6 7 11 7 5 6 5 8 7 10 5 8\n'
It becomes a character string of R console-like output.
python
re.sub(r' +', ',' ,(re.sub(r'\[.+?\]', '', return_str.split(')')[-1].replace('\n','')).strip())).split(",")
#The result is a string so cast required
So I bite this every time
Is there a way to make the return value more usable? ..
Recommended Posts