Lab 1: Introduction to R and R Markdown


Setup

Make a Course Folder

If you have not already, create a folder on your computer to dedicate to this course.

Create your Lab 1 File

  1. Open RStudio.cloud or RStudio on your computer (preferably RStudio on your computer!)

  2. In RStudio, go to “File” > “New File…” > “R Markdown…” and click “OK” in the dialog without changing anything.

  3. Delete all the placeholder text in that new file and replace it with this:

    ---
    title: "Lab 1"
    author: "Put your name here"
    output: html_document
    ---
    
    
    # Setup
    
    ```{r load-libraries-data, include = FALSE}
    library(tidyverse)
    
    cars <- ggplot2::mpg
    ```
    
    ## About the dataset
    
    This dataset contains information about ______.
    
    It has _____ rows and ______ variables.
    
    # Plot 1
    
    Replace this line with a code chunk and use it to create a plot.
    
    # Plot 2
    
    # Plot 3
    
  4. Save the R Markdown file with some sort of name (without any spaces!). Make sure you save it somewhere in your new course folder.

Lab Instructions

Run the provided code

Click the “Play” button on the right of the first code chunk to run the code. Alternatively, you can highlight the code you want to run and type ctrl + Enter or ⌘< + Enter. You should see the lines of code appear in the console, and a data frame named cars should appear in your environment.

Check the data documentation

In your console (NOT in the R Markdown document), type ?ggplot2::mpg.

Use the information that pops up in your Help pane in RStudio to fill in the blanks in the Dataset Information section.

Make a code chunk

Remove the text that says “Replace this line with a code chunk” and insert a new R code chunk. Either type ctrl + alt + i on Windows, or + + i on macOS, or use the “Insert Chunk” menu:

Create and discuss some visualizations

Use ggplot() to create three plots using the mpg dataset.

  • Plot 1: Summarize one categorical variable, with a bar plot.

  • Plot 2: Compare two quantitative (numeric) variables, with a scatter plot.

  • Plot 3: Compare a quantitative variable across categories with a side-by-side box plot.

Type the code to create each new plot in a new empty chunk, in the appropriate section of your document.

For full credit, make sure your plots have reasonable axis labels. For example, if you use the fl variable, your axis should say “Fuel Type” not “fl”.

In regular text (possibly using Markdown formatting!), type a very brief interpretation of the plot. What does it show? What do you learn about cars from this plot?

Knit your document

Knit your document as an html file. Use the “Knit” menu:

If you run into trouble knitting, try running your code again in order, and see if you can find the problem.

Another common issue is deleting the tick marks (/```) that surrpund your code chunks. If you notice that the code chunks are not showing a “play” button, or that they are not highlighted in grey, double check your tick marks!

You’ll be doing this same process for all your future Lab Assignments and Practice Activities. Each of these will involve an R Markdown file.

We will discuss easy ways to manage all these documents starting next week.

Challenge:

Pretty R Markdown documents

There are infinitely many ways to make R Markdown documents look prettier or fancier. In fact, that’s how I create all the coursework and assignment documents for this class!

Read through this guide, and make sure to check out the themes and templates sections.

Apply a new theme or template, and any other tricks you want, to your Lab 1 document.