R and Python in one Jupyter notebook
Asked Answered
H

5

84

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?

Hiroko answered 18/8, 2016 at 0:9 Comment(0)
O
104

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.


You also have access to R objects (e.g. data frames) from Python cells:

import rpy2.robjects as robjects
robjects.globalenv['some-variable-name']

To view the names of all available variables use:

list(robjects.globalenv.keys())

Details are explained here: Pandas - how to convert r dataframe back to pandas?

Ostracize answered 27/1, 2017 at 8:50 Comment(4)
When I try this, I get an error that says: Error in library(ggplot2) : there is no package called ‘ggplot2’Dakota
This is brilliant! Please buy yourself a cup of coffee - on me.Hurdygurdy
How do you get any output from the R cell back to python ?Braggadocio
Thank you! BTW, if there is a problem with rpy2 (kernel crashing, i had that in VScode) here is how to fix it: https://mcmap.net/q/244168/-r-kernel-crashes-while-loading-r-package-using-rpy2Avidity
S
26

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.

Silvery answered 17/1, 2018 at 0:6 Comment(0)
C
14

UPDATE April 2018,

RStudio has also put out a package: https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/

for which it is possible to run multiple code chunks in different languages using the R markdown notebook, which is similar to a jupyter notebook.

In my previous post, I said that the underlying representation of objects is different. Actually here is a more nuanced discussion of the underlying matrix representation of R and python from the same package: https://rstudio.github.io/reticulate/articles/arrays.html

Old post:

It will be hard for you to use both R and Python syntax in the same notebook, mostly because the underlying representation of objects in the two languages are different. That said, there is a project that does try to allow conversion of objects and different languages in the same notebook: http://beakernotebook.com/features

I haven't used it myself but it looks promising

Cocklebur answered 18/8, 2016 at 0:15 Comment(0)
L
8

SoS kernel is another option.

Don't know how well it performs yet, just started using it.

The SoS kernel allows you to run different languages within the same notebook, including Python and R.

SoS Polyglot Notebook - Instructions for Installing Desired Languages

Here is an example of a notebook with Python and R cells.


*Update:

In terms of sharing variables, one can use the magics %use and %with. "SoS automatically shares variables with names starting with sos among all subkernels"1.

Ex.

Starting cell in R:

%use R
sos_var=read.csv('G:\\Somefile.csv')
dim(sos_var)

Output:

51  13

Switching to python:

%with Python3
sos_var.shape

Output:

(51, 13)
Legit answered 22/8, 2018 at 17:18 Comment(1)
sos looks nice, but currently the practical use for a developer looks limited. E.g. if you do %get jl --from julia-1.0 print( jl ) -> Subkernel julia-1.0 does not support magic %put. If you use conda it's had to combine Python and R in one environment.Dmz
C
0

A small addition to @uut's answer and @msh's comment: If you are using rpy2 in Jupyter Notebooks you also have access to R objects (e.g. data frames) from Python cells:

import rpy2.robjects as robjects
robjects.globalenv['some-variable-name']

To view the names of all available variables use:

list(robjects.globalenv.keys())

Details are explained here: Pandas - how to convert r dataframe back to pandas?

Capello answered 10/1, 2022 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.