Running R and Python in Jupyter
The Jupyter Project began in 2014 for interactive and scientific computing. Fast forward 5 years and now Jupyter is one of the most widely adopted Data Science IDE's on the market and gives the user access to Python and R
The Jupyter Project started in 2014 as an initiative towards interactive and scientific computing.
It included a Python kernel so that the user would have a new an interactive IDE to use Python.
It is based on the IPython computing environment. In 2015, support for R was also added with the IRkernel. This allows users to run kernels with R and Python.
R Kernel Installation
If you are using Windows, the following method is fine. If you are using a Unix based machine (OSX or Linux), THE COMMANDS MUST BE RUN IN THE TERMINAL.
The best way is to install Anaconda which will automatically have an installation of Python, R and Jupyter Notebooks.
- After Anaconda installation, open the Anaconda Prompt and type
install.packages(c(repr, IRdisplay, evaluate, crayon, pbdZMQ, devtools, uuid, digest), type=source). It is imperative that this command be done in the terminal to execute directly from R and not an IDE.
- After Step 1, run
- Go to the Anaconda Navigator and open Jupyter Notebook or type
jupyter notebookin the Anaconda Prompt. Under New you should find an R kernel. Click on that to start running R in the Jupyter environment.
A word of caution. Since installing Anaconda is the easiest and fastest way to have Jupyter Notebook installed, the R Version that Anaconda installs lags behind the latest release. You cannot update R in Anaconda the same way you would if you installed it separately from the R website. This is because all the programs in Anaconda is managed by the Conda package manager and Anaconda needs to update their default version of R before the users get to install it. So when running R in Jupyter Notebooks, some packages may not work (although this is rare) in the older version due to package updates happening more frequently than R updates.
Running R in Jupyter
Now that we have installed R in Jupyter, we can start using R code as we normally would in any IDE for R.
- After Step 3 above, you will be taken to a new tab with an R kernel ready:
- Next, give your kernel a name so that you can identify it for later. You merely click where it says Untitled and you will be able to give it a name. After this, you are ready to start using R in Jupyter.
- In the first code block execute
library(tidyverse), data(mtcars), options(warnings=-1).options(warnings=-1)will suppress any warning messages so use it only after you ran code chunk. Press Ctrl+Enter in Windows or Cmd + Enter in Mac to run a code chunk.
glimpse(mtcars)gives an verview of the dataset with data types and dimensions and
summary(mtcars)generates summary statistics on all the features of the dataset.
options(repr.plot.width=5, repr.plot.height=5)sets the global options for plots to be the size as specified. If different plots need to be resized, adding the same line of code to a different chunk will cause the plot executed from that chunk to resize.
Now Python is more straightforward in Jupyter. After installing Anaconda as shown above, a Python kernel is the default installation. When you open Jupyter Notebook, go to New --> Python and you will be taken to a Python environment in Jupyter.
If you were already in the R kernel (or any other kernel), go to File --> New Notebook --> Python 3
This will take you to a new tab in your browser with a Python environment ready. Now you have both R and Python running in Jupyter at the same time. You can run both at the same time or one after the other (as you wish).
This is to get you started with R and Python in Jupyter. You can work in R as you normally would. There are more to explore in Jupyter. An upcoming tutorial will focus on how to report with Jupyter using Markdown and nb-extensions.
- Best Practices for Using Notebooks for Data Science
- Remote Data Science: How to Send R and Python Execution to SQL Server from Jupyter Notebooks
- Top 5 Best Jupyter Notebook Extensions