An R Notebook is an R Markdown document with chunks that can be executed independently and interactively, with output visible immediately beneath the input. R Notebooks are a method of literate programming that allows for direct interaction with R while creating a reproducible document with publication-quality output.
A notebook for this class will consist of comments and directions followed by a chunk of code that can be ran independently in order for students to see the examples and follow along with the lessons.
To use R Notebooks, first R Studio must be opened. To create an R Notebook in RStudio, go to File -> New File -> R Notebook. In this course though, the notebooks will be created before the start of class and students will edit them throughout the lab.
Typically, a data set or notebook will be downloaded and it is important to set this folder as the working directory for the project prior to any coding. This can be done by going to Session -> Set Working Directory -> To Source File Location
The header is the very first few lines of code that give important information about the document. It is essential to edit this information everytime to ensure you are credited for your work and so people looking at your code know when and where it is coming from.
Notebook chunks can be inserted quickly using the keyboard shortcut Ctrl + Alt + I (OS X: Cmd + Option + I), or via the Insert command in the editor toolbar found at the top.
A chunk of empty code looks like this in the notebook before it is published:
It will always begin with they ``` symbol followed by the {r}
and ending with the ```. In this light grey box, you can code after the {r}
and before the second ``` symbol. You can insert as many chunks of code as you would like as it is user preference. Typically, in an R Notebook, we aspire to have one output per chunk of code. If there will be more than one output, it is best to split the code.
In order to run the code, you have several options.
Use Ctrl + Shift * Enter (OS X: Cmd + Shift + Enter) or the Run Chunk command, , to run the current chunk.
Press Ctrl + Enter (OS X: Cmd + Enter) to run just the current statement. Running a single statement is much like running an entire chunk consisting only of that statement.
Use the Run All and Run Previous commands to run a batch of chunks found at the very top.
After downloading a data set, we must read the data in. So, create an R code chunk in the R Notebook and write the code to read in the file. To make sure the file is read correctly, run the chunk. The code should look something like this.
The green triangle with the red square around button is the ‘run’ button. If selected, this button runs the code. If we push it, we should obtain the following result.
This is the output. If the output is not what you want, or if there is an error, you can simply go back to the code chunk, edit the code, and rerun it.
When coding, there are often small errors that are made. With R Notebooks, the message will display exactly what line your error is on, which allows you to fix it.
In the example below, we see that the error is made because we forgot to set the working directory. This is a very common mistake and as a programmer, we will learn to identify these errors quickly.
When working on a lab, it is important to leave comments within your code to understand what you are doing when you refer to it later. A comment is made in the grey code chunk box by putting a “#” symbol in front of text. This is what a comment will look like:
Throughout the lab sessions, you will be asked to take screenshots of your work and upload them to the lab before submitting your work. Additionally, it may be nice to add images to your notebook to enhance your code or understanding of the project.
To add a picture, use the following command and preview it.

Below is an example of how this should look.

This is a caption
After completing a lab, you will be submitting a HTML link to Sakai for evaluation. If you are using R online, it is a simple process which involves creating an account on http://rpubs.com.
After creating an R Notebook or R Markdown file, it is important to publish your work on RPubs. At the top left, there is a button that says “knit”. If we click on it, a drop down box should appear. We want to save it as an HTML file, so we click “Knit to HTML”.
If this is the first time, you will probably get a pop-up window asking you to install the required packages. Answer ‘Yes’.
This will result in a HTML document. In the top right corner, there will be an option to “Publish” the document.
Again if this is the first time, you will probably get a pop-up windown asking you to install the required packages. Answer ‘Yes’.
A window will pop up, following the installation of a few packages. Make sure to publish to “RPubs”.
You will need to hit publish on the screen that looks like this:
That will bring you to the following page:
Follow the directions to “Create an Account”. After doing so, you will be lead to “Step 2 of 2”. It is recommended that you assign the same Title name and URL name extension in reference to the convention adopted in class when saving the document. For example in this case the name is chosen to be bsad_lab01 in reference to lab session 01.The URL can be pasted and uploaded to Sakai.
R Notebooks are revolutionary as they allow students to edit within the file and see the changes that are made with the output immediately without have to run the entire code. Please refer to this guide or the instructor with other questions.