The lab you are reading is meant to briefly introduce you to how our labs will work this quarter. In a nutshell, you will read a file like this one that contains sample code and instructions. In that file will be instructions to complete work or answer questions in your lab report. That work is done on the lab template file that you must download from D2L and upload to RStudio.

Some questions in the lab simply require you to answer the question. In that case, type the question and answer. Most questions, however, require you to execute R commands. Those commands should be entered into the notebook in what are called code chunks. When you execute code within the notebook, the results appear beneath the code.

Inserting Code Chunks

Code can be run in an R Notebook by inserting a “code chunk.” To add a code chunk, click the Insert Chunk button on the toolbar.

Now do Exercise 1.

Now do Exercise 2.

Loading Data

R contains many prebuilt data sets that can be easily added. Let’s see how to do that. Copy the following command and paste it into the “console” found at the bottom left of your R studio screen and press enter.

source("http://www.openintro.org/stat/data/arbuthnot.R")

This loads a data set from the internet. You should see the arbuthnot data set in the environment box at the top right of your screen.

Now do Exercise 3

Insert a new chunk and type the command summary(arbuthnot). Then press the run button within the chunk. You should see a summary of the arbuthnot data frame.

Making an output file

When you are done answering all the lab questions, you create an output file which you will upload to D2L for our TA to grade. Let’s practice that process. Warning: You will get an error the first time. Keep reading to learn what caused the error, how to fix it, and how to make a pdf file of your work to turn in.

Go to your lab report file and choose the down arrow next to the knit button in the toolbar. Choose “knit to pdf” to create a pdf output. You should get an error. If you look through the red text, you’ll see that the object ‘arbuthnot’ was not found. Why?

When you load data or create a variable in the Console, it adds that data to the environment. When you run an individual code chunk in a notebook, that chunk has access to things in the environment. That is why there was not error when you ran the code chunk containing the summary(arbuthnot) command.

However, when you use the knit command, the whole process starts from scratch. The commands in the notebook are run one after the other, but the notebook only sees commands that are actually typed in the notebook. Since you didn’t load the arbuthnot data set in the notebook, the notebook can’t find it when you type knit.

Now do Exercise 4

You are now done with lab for week 1!