{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 01 - Setting Up Jupyter and R\n", "\n", "Marina Adshade, Paul Corcuera, Giulia Lo Forte, Jane Platt \n", "2024-05-29\n", "\n", "## Prerequisites\n", "\n", "1. Have Anaconda Navigator installed onto your personal computer.\n", "\n", "## Learning Outcomes\n", "\n", "1. Connect to JupyterLab using an R session.\n", "\n", "## 1.1 Jupyter\n", "\n", "We will need to install [Anaconda Navigator](https://www.anaconda.com/)\n", "in order to be able to run our files in JupyterLab.\n", "\n", "Conda is an open-source package and environment management system. With\n", "Conda, we can create a particular directory folder (also known as\n", "environment) that will contain the packages that allow us to run Jupyter\n", "online notebooks. These notebooks run code coming from different\n", "softwares (henceforth referred as kernels): Stata, R, Python, etc. The\n", "bare minimum for our environments is some version of Python.\n", "\n", "The only way to set up an environment based on R requires us to manually\n", "connect our computer’s R program to Jupyter notebooks.\n", "\n", "**Note:** We only have to do this once on any computer. After that we\n", "can go directly to Jupyter Notebooks for our work.\n", "\n", "## 1.2 Setting Up Our Computer\n", "\n", "Once Anaconda has been installed, launch JupyterLab from Anaconda\n", "Navigator. Within JupyterLab, click on the plus sign in the blue box on\n", "the top right corner and from the page that appears select “Terminal”.\n", "All commands given below will be typed into that terminal window in the\n", "box that follows the `$`.\n", "\n", "Once we have the terminal open, we can run the `conda` commands that\n", "find packages to install. This is not dissimilar to downloading software\n", "from a server. We want to make sure that the computer first finds the\n", "`conda-forge` channel which contains packages.\n", "\n", "To do this, we should run the following commands directly in our own\n", "terminal window in JupyterLab:\n", "\n", "``` bash\n", " conda config --add channels conda-forge\n", " conda config --set channel_priority strict\n", "```\n", "\n", "The goal here is to create a package bundle, i.e. an environment, where\n", "we will install some version of R, Stata Kernel, and Jupyter. You can\n", "explore the things you can download to an environment from the\n", "`conda-forge` channel by running, for example, `conda search r-base`,\n", "`conda search stata_kernel`, etc. That way, you can see all the\n", "different versions of these packages that you can download from the\n", "different channels.\n", "\n", "Now we are ready to create a new environment where we will install all\n", "these packages. In this particular case, we will create an environment\n", "based on Python 3.9.7. Let us create an environment called `stata_r_env`\n", "by writing:\n", "\n", "``` bash\n", " conda create -n stata_r_env python=3.9.7\n", "```\n", "\n", "If we omit the `=3.9.7` part, we will create an environment with the\n", "default Python version.\n", "\n", "We want anything that we install from the channel to be part of this new\n", "environment. To do so, we need to activate it by running\n", "\n", "``` bash\n", " conda activate stata_r_env\n", "```\n", "\n", "Now that our environment is activated, we can install everything we\n", "want. We begin by installing Jupyter, which will allow us to run the\n", "interactive notebooks:\n", "\n", "``` bash\n", " conda install jupyter\n", "```\n", "\n", "## 1.3 Installing R Kernel on an Environment\n", "\n", "Finally, to be able to run the entire ECON 490 folder, it is highly\n", "recommended to install a stable R-version. In this particular case, we\n", "will focus on R 4.1.2. We can do this by running:\n", "\n", "``` bash\n", " conda install -c conda-forge r-base=4.1.2 \n", "```\n", "\n", "To use R interactively from this environment, we need to open R from the\n", "terminal. If you are using Windows, you can do this by typing `r.exe` or\n", "`start r`. If you are using MacOS, you can do this by typing `r`. You\n", "will notice that a message pops up, similar to when we open R from our\n", "desktop. You need to run:\n", "\n", "``` bash\n", " install.packages('IRkernel')\n", " IRkernel::installspec()\n", " q()\n", "```\n", "\n", "The first two lines connect our R version with the Jupyter notebook. The\n", "last line closes R from the terminal because we’re done. Now we should\n", "be able to change directories from the terminal by running\n", "`cd any_directory` and then running `jupyter notebook` to open the\n", "interactive web page needed to open, create and export Jupyter\n", "notebooks.\n", "\n", "## 1.4 Running the COMET Notebooks on Your Own Computer\n", "\n", "Now that we have installed the Stata kernel and successfully connected\n", "our own version of Stata to the notebooks, we may want to run the COMET\n", "notebooks locally on our computer.\n", "\n", "To do so, just follow some simple steps:\n", "\n", "1. **Download the notebooks from COMET.**\n", "\n", "- On the top-right corner of this webpage, we can see the menu called\n", " **“LAUNCH COMET”**. Click on the down arrow to its right and then\n", " click on **“LAUNCH LOCALLY”**.\n", "- A zipped folder will be automatically download to our computer.\n", " Unzip it, and within it locate a folder called `econ490`.\n", "- That folder contains a subfolder `econ490-stata` with all Jupyter\n", " Notebooks concerning the Stata modules and a subfolder `econ490-r`\n", " with all the Jupyter Notebooks concerning the R modules. Move the\n", " whole `econ490` folder to where it is most convenient for you on\n", " your computer. You will need also some of the material contained in\n", " the `econ490-stata` folder.\n", "\n", "1. **Open the notebooks in Jupyter.**\n", "\n", "- Open Anaconda Navigator and locate the Jupyter notebook tile.\n", "- Click on the button “Launch” in the Jupyter notebook tile. A Jupyter\n", " file browser will open in a web browser tab.\n", "- Click on the File Browser on your left (a folder icon) and locate\n", " your `econ490` folder. Now open any module you may want to work on!\n", "- Be careful! Always make sure that the R kernel is connected and\n", " ready to run.\n", " - We may check the status of our kernel by looking at the circle\n", " on the top-right of our Notebook. It should be of color white.\n", " - Moving our cursor on top of it, we should see the message\n", " `Kernel status: Idle`.\n", " - The first time we open a R notebook, it will take a couple of\n", " seconds for the R kernel to connect. While connecting, the\n", " circle will be gray with a tiny thunderbolt inside.\n", " - We can always interrupt or reconnect the kernel by clicking on\n", " the “Kernel” menu on the top bar.\n", "\n", "**Note:** We only have to do step 1 once on any computer. After that, we\n", "will only have to do step 2: open directly the notebooks in Jupyter.\n", "\n", "We can also choose to run the R modules of the COMET notebooks online.\n", "To do so, go on the **“LAUNCH COMET”** button located on top-right\n", "corner of this webpage and then click **“LAUNCH ON JUPYTEROPEN”**. You\n", "will need to input your CWL credentials and then a Jupyter file browser\n", "will open in a web browser tab. Navigate through the folders in the\n", "`Files` tab: click on folder `docs`, then `econ_490`, and finally\n", "`econ490-r`. Now open any module you want to work on.\n", "\n", "**Note:** Please speak with your instructor if you run into any issues\n", "with setting up JupyterLab." ], "id": "20d1ef06-0a13-439a-a0d0-6ec23d927654" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "ir", "display_name": "R", "language": "r" } } }