R with Python and Jupyter Notebook¶
Download and install R¶
First, you'll want to install R – go here to read the installation instructions.
Use R in Jupyter notebooks¶
Run the code below from the command land. It starts up R, installs the adapter between Jupyter and R (a kernel), and then exits.
R -e "install.packages('IRkernel', repos = 'http://cran.us.r-project.org');IRkernel::installspec()"
Now when you open Jupyter you can create new R notebooks!
R and Python in one notebook¶
Install the rpy2 package, which allows Python to talk to R. You can just use pip
to install it:
pip install rpy2
Each time you start a new notebook you'll need to load rpy2 as an extension:
In [1]:
Copied!
%load_ext rpy2.ipython
%load_ext rpy2.ipython
And then you can run all of the R code you want, as long as you put %%R
in the top of the cell.
In [2]:
Copied!
%%R
R.version.string
%%R
R.version.string
[1] "R version 4.2.2 (2022-10-31)"
Sharing variables between Python and R¶
You'll start with a pandas dataframe.
In [5]:
Copied!
import pandas as pd
df = pd.read_csv("countries.csv")
df.head()
import pandas as pd
df = pd.read_csv("countries.csv")
df.head()
Out[5]:
Country | Continent | GDP_per_capita | life_expectancy | Population | |
---|---|---|---|---|---|
0 | Afghanistan | Asia | 663 | 54.863 | 22856302 |
1 | Albania | Europe | 4195 | 74.200 | 3071856 |
2 | Algeria | Africa | 5098 | 68.963 | 30533827 |
3 | Angola | Africa | 2446 | 45.234 | 13926373 |
4 | Antigua and Barbuda | N. America | 12738 | 73.544 | 77656 |
Then you'll import it into your R cell with -i df
at the top of the cell. Now R can use df
just like normal!
In [22]:
Copied!
%%R -i df
library("ggplot2")
ggplot(df, aes(x=GDP_per_capita,
y=life_expectancy,
color=Continent)) +
geom_point()
%%R -i df
library("ggplot2")
ggplot(df, aes(x=GDP_per_capita,
y=life_expectancy,
color=Continent)) +
geom_point()