Sharing data among Python, R, Java, Javascript in Jupyter notebook
Tags: jupyter, python, r, javascript
Published: 2020-03-05 08:57:35

This will be the conclusion of my journey of exploring polyglot data visualization. As a scientific software engineer, there are many chances to cope with various kinds of environments where scientists are keen on. Personally, I prefer to using javascript because it is platform agnostic. 

I have one post how to communicate between python and javascript/java thanks to BeakerX extension. {% link hkmoon/autotranslation-from-python-to-javascript-in-jupyter-40cc %}

Today we take a look at how to exchange data between R and Python cells in Jupyter. The summary figure shows how to share data among R, Python, Java, Javascript in Jupyter notebook.

![Summary](https://dev-to-uploads.s3.amazonaws.com/i/13w21iix8b3bm2kzmrt2.png)

### rpy2 (https://rpy2.readthedocs.io/en/latest/)

_rpy2_ is designed to facilitate the use of R by Python programmers. R objects are exposed as instances of python classes and R functions as bound methods to those objects. 

In order to use _rpy2_ in jupyter notebooks, we need to install them first. Please, refer https://rpy2.readthedocs.io/en/latest/overview.html#installation.

Then, enable it with _load_ext_ magic in the jupyter cell.
```python
%load_ext rpy2.ipython
```

For the convenience, let's put this extension in _ipython\_config.py_ file which is located in _~/.ipython/profile_defaults_. If there is no config file in the location, please, create _ipython\_config.py_. Then, paste the below content.
```python
c.IPKernelApp.extensions = [
    'rpy2.ipython'
]
```
Now you can freely use the magics provided by _rpy2_. Let's check the output of _pi_ from R.
```python
import rpy2.robjects as robjects
pi = robjects.r['pi']
pi[0]
```

1. Create a dataframe in Python
    ```python
    import pandas as pd
    python_df = pd.DataFrame({'A': [2, 3, 5, 2, 3, 4, 6, 2, 5, 9],
                              'B': [4, 2, 4, 6, 2, 4, 6, 7, 3, 5]})
    
    print(python_df)
    ```
2. Use the dataframe in R with _-i_ flag in the R magic
    ```R
    %%R -i python_df
    # Pass the specific dataframe to R using the -i option
    
    plot(python_df, main="Plotting a Python Dataframe in R",
        col="red", pch=12)
    ```
3. Export R variables with _-o_ flag in the R magic
    ```R
    %%R -o model -o coef
    # R to Python
    
    model <- lm(B ~ A, data=python_df)
    coef <- model$coefficients
    ```
4. Use the exported R variables with _rpy2_
    ```python
    import rpy2.robjects as robjects
    
    print(model)
    print(type(model))
    
    print(dict(model.items()))
    ```

Normally, the exported R dataframe is automatically translated as a panda's dataframe in Python.
```R
%%R -o cars_df

cars_df <- mtcars
```

```python
# print(cars_df)
from beakerx import *
from beakerx.object import beakerx

beakerx.pandas_display_default()
cars_df
```

However, there is no direct way from R to Java/Javascript now. Hopefully, BeakerX team will provide the auto-translation feature for R sooner or later.

Here is the gist including all the snippets shown in the article.

{% gist https://gist.github.com/hkmoon/39ebf6b794cedf9ca05edc7dd209df5f %}

Happy coding!