I want to use R functions easily with ipython notebook

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

I want to use R functions easily with ipython notebook
I want to use a virtual environment with jupyter notebook!
I want to blog with Jupyter Notebook
I want to use IPython Qt Console
I want to use an external library with IBM Cloud Functions
I want to use MATLAB feval with python
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
How to use IPython Notebook
Use Bokeh with IPython Notebook
I want to use the R dataset in python
R & D life with iPython notebook
I want to easily delete columns containing NA in R
[Python] I want to use the -h option with argparse
I want to announce my graduation thesis on IPython Notebook
I want to do ○○ with Pandas
I want to debug with Python
I want to use a wildcard that I want to shell with Python remove
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
I want to detect objects with OpenCV
Use apache Spark with jupyter notebook (IPython notebook)
I want to use jar from python
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
How to use jupyter notebook with ABCI
I wanted to use jupyter notebook with docker in pip environment (opticspy)
I tried to use lightGBM, xgboost with Boruta
How to debug with Jupyter or iPython Notebook
I want to easily create a Noise Model
I want to analyze songs with Spotify API 2
Anyway, I want to check JSON data easily
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to be an OREMO with setParam!
I want to analyze songs with Spotify API 1
I want to use ceres solver from python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to easily find a delicious restaurant
I want to write to a file with Python
I want to use the activation function Mish
I want to use mkl with numpy and scipy under pyenv + poetry environment
How to use IPython
I want to convert an image to WebP with lollipop
I want to easily implement a timeout in python
I want to detect unauthorized login to facebook with Jubatus (1)
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I want to inherit to the back with python dataclass
I want to easily build a model-based development environment
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!