3 Run R & Python
3.1 Option 1: Run R & Python on Web
Select ONE of the following options and run R and Python scripts without installing anything on your system.
3.1.1 Google Collaboratory
Go to Google Collaboratory.
You will need a Google account to use Google Collaboratory.
Create a Google account when prompted or log into your account
Click
New Notebook
if the prompt shows orFile > New Notebook
if presented with the Welcome to Colaboratory notebook.You now can run Python code directly in the cells, press the
Run
button (Play
icon) to execute the code cell orShift + Enter
.To run R code insert a new code cell, write
%load_ext rpy2.ipython
and run the cell to load the R kernelOnce the kernel is loaded, write
%%R
at the top of every new code cell that includes the R code.Execute R codes in each cell as the standard way.
You can run R and Python code in the same notebook but add the
%%R
when you wish to run R scripts.
3.1.2 Jupyter Notebooks
3.1.2.1 Running Python scripts
Go to Jupyter Notebooks website.
Click Jupyter Notebook image (under Applications) to start a demo Python Notebook page.
Once loaded, it will present an example Python notebook.
Click
File > New > Notebook
to create a new notebook with Python kernel.
3.1.2.2 Running R scripts
Go to Jupyter Notebooks website.
Scroll down to ‘Kernels’ and click the R image to start a demo R Notebook page.
Note it may take some time to start the R repository.
Once loaded, it will present an example R notebook
Click
File > New Notebook > R
to create a new notebook with R kernel (check the right-hand corner that it shows the R kernel)Note unlike Google Collaboratory, you cannot run Python and R in the same notebook.
You can now move between two tabs on the browser to use both R and Python notebooks.
You do not need an account to use Jupyter Notebook
Here is a quick summary of Jupyter Notebook shortcuts.
3.1.3 CoCalc
Go to CoCalc website.
You will need a CoCalc account to use this resource
Create a new account or log into your account
Head to the link, and give your project a name, then click
Create Project
Next click
+ New
from the tabs at the top of the page, then selectJupyter Notebook
from the list.Then choose either
Python
orR
kernel.You can enter your code in the code cells and execute that cell by pressing the
Play
button orShift + Enter
.
3.2 Option 2: Run R & Python Locally
Follow Steps 1 to 4 depending on your preferences.
Depending on your systems, it may still require additional configurations to integrate all environments that talk with each other seamlessly.
Please note we cannot cover or support technical issues related to installation or configuration during the workshop.
3.2.1 Step 1: Download R
Download and install R.
3.2.2 Step 2: Download Python
Optionally, download and install Python.
For the workshop, however, download and install Anaconda Distribution which is a user-friendly popular Python distribution platform.
Anaconda includes an integrated and typical Python environment and comes with several important Python packages.
Go to Step 3 or 4 depending on your familiarity with one of the following integrated development environments (IDE):
RStudio (Go to Step 3)
Jupyter Notebook (Go to Step 4)
3.2.3 Step 3: R Users
This step is probably applicable to users who work predominantly in R.
3.2.3.1 (A) Download RStudio
Download and install RStudio; it is the most popular integrated development environment (IDE) for R. If you already use RStudio, skip this step. Go to Step 3B.
3.2.3.2 (B) Install the R package reticulate
Install the package reticulate, an interface to ‘Python’ from RStudio IDE.
install.packages('reticulate')
use_python("/usr/local/bin/python")
3.2.4 Step 4: Python Users
This step is probably applicable to users who work predominantly in Python.
Steps to follow with standard Anaconda distribution (from Step 2)
Using the Anaconda distribution is the simple and easier option for the workshop content.
• Open the Anaconda navigator select the Environments
tab and click Create
• Give your environment a name and select either Python or R.
• Once created install Jupyter Notebook from the panel on the right.
• Now click the Play
button on the environment you just created and select Open with Jupyter Notebook
• If you wish to install any other packages like plotnine
, you can select Open with Terminal
from the previous step and use the pip
command to install packages.
- Install plotnine:
pip install plotnine
Here is a quick summary of Jupyter Notebook shortcuts.
Optional: Steps to follow with standard Python distribution (from Step 2)
3.2.4.1 (A) Install Jupyter Notebook
jupyterlab is an interactive environment for many programming languages including R and Python.
pip install jupyterlab
3.2.4.2 (B) Install required Python packages
pip install numpy
pip install numpy
pip install plotnine
pip install statsmodel
pip install scikit-learn
3.2.4.3 (C) Optional: Other Python IDE
A list of other excellent integrated development environments (IDE) for Python:
IDLE: IDLE is Python’s Integrated Development and Learning Environment bundled with Python installation.
spyder: The Scientific Python Development Environment
PyCharm: A free version of IDE available from jetbrains.com.
Visual Studio Code A versatile IDE for multiple languages from Microsoft. Check this link to run R in Visual Studio Code.
Note: The above IDEs do not have direct R integration.