Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R and Python in one Jupyter notebook

Is it possible to run R and Python code in the same Jupyter notebook. What are all the alternatives available?

  1. Install r-essentials and create R notebooks in Jupyter.
  2. Install rpy2 and use rmagic functions.
  3. Use a beaker notebook.

Which of above 3 options is reliable to run Python and R code snippets (sharing variables and visualizations) or is there a better option already?

like image 661
Sailendra Pinupolu Avatar asked Aug 18 '16 00:08

Sailendra Pinupolu


People also ask

Can I use R and Python in the same Jupyter Notebook?

Create a notebook file for use with R and Python Select the R instance where you want to install dependencies. Click Open JupyterLab. Select File > New > Notebook. Select the Python 3 kernel for your new notebook file.

How do I use Python and R in Jupyter?

Click the green play button on the r-tutorial environment and select the Open with Jupyter Notebook option. To create a new notebook for the R language, in the Jupyter Notebook menu, select New, then select R. To run the code, in the menu bar, click Cell then select Run Cells, or use the keyboard shortcut Ctrl-Enter.

Can you use R and Python together?

Using R and Python together at the same time is incredibly easy if you already have your R scripts prepared. Calling them from Python boils down to a single line of code.

Can I open R in Jupyter Notebook?

Using R in a Jupyter NotebookTo run a Jupyter Notebook with R, you need to create a conda environment and activate the kernel so Jupyter can recognize it. Then you can work with the R language in a notebook.


2 Answers

Yes, it is possible! Use rpy2.

You can install rpy2 with: pip install rpy2

Then run %load_ext rpy2.ipython in one of your cells. (You only have to run this once.)

Now you can do the following:

Python cell:

# enables the %%R magic, not necessary if you've already done this %load_ext rpy2.ipython  import pandas as pd df = pd.DataFrame({     'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],     'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1] }) 

R cell:

%%R -i df -w 5 -h 5 --units in -r 200 # import df from global environment # make default figure size 5 by 5 inches with 200 dpi resolution  install.packages("ggplot2", repos='http://cran.us.r-project.org', quiet=TRUE) library(ggplot2) ggplot(df, aes(x=cups_of_coffee, y=productivity)) + geom_line() 

And you'll get your pretty figure plotting data from a python Pandas DataFrame.

like image 78
uut Avatar answered Sep 18 '22 23:09

uut


Using @uut's answer for running R in a jupyter notebook within python kernel (in MacOS), the following worked for me.

%%Rshould always be at the start of the cell else you will get the error as shown in figure belowsyntax error if %%R not at the top of the cell

The following is the right way:Right way to invoke R within python kernel

Also %load_ext rpy2.ipython should come before %%R hence put it in a different cell above it as shown in the figures.

like image 33
Abhimanu Kumar Avatar answered Sep 19 '22 23:09

Abhimanu Kumar