R Markdown Practice

Activity 01

Author

Willie Wildcat

Published

September 23, 2022

Note

This html solution shows the code chunk settings so that you can easily review your solutions. Your html report will not display the #| settings or ```{r} part of the code chunk.

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Loading Package

```{r}
#| label: load-pkgs
#| message: false
library(nycflights13)
library(dplyr)
library(knitr)
```

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}

1 + 1
```
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

Tasks

Complete the following series of tasks. Remember to render early and render often.

Task 1

It will be easier for you to compare your qmd file to the html file if we make a slight change to the RStudio settings.

  • Find the gear/sprocket icon next to the Render button.
  • Click it and then select Preview in Viewer Pane.
  • Render the document to see the change.

Now take some time to compare what you see in this document with the html version that is now previewed in the viewer pane.

Task 2

Let’s make some adjustments to the document:

  1. Change the title to “R Markdown Practice”.
  2. Add an “Activity 01” as a subtitle. Hint subtitle: goes right below title:.
  3. Change the author to your name.
  4. Change the date to today’s date in “YYYY-MM-DD” format. Example 9/1/2030 becomes 2030-09-01.

Task 3

Insert an R code chunk BELOW and name it task3. Place the following two lines of code in it:

```{r}
#| label: task3
x <- 9
y <- 45
```

The <- is an assignment operator. Here it assigned the number 9 to x and the number 45 to y. You store values so that you can access them by name later!

Task 4

Change the R code chunk options below to #| eval: true or alternatively you can simply remove #| eval: false from the code chunk options. Either will have the same effect. This depends on completing Task 3 correctly.

```{r}
#| label: task 4
#| eval: true
x * y
```
[1] 405

Task 5

Let’s focus on R code chunks. You may have noticed a down arrow and a green arrow on the upper right side of the code chunk.

Click the green arrow on the task4 R code chunk.

An error MIGHT pop up right below the R chunk (if you did not get an error that is OKAY - you are one step ahead of us).

Before we address the potential error let’s change an RStudio setting. Again, find the gear/sprocket next to the Knit button. Click it and select Chunk Output in Console. A pop-up should appear, select remove output. Now click the green arrow on the task4 R code chunk. Any ouput or message should now appear in the console below.

Now let’s take care of the error. Note that the error doesn’t appear when you knit the document. All the code works fine. Test this out if you haven’t already. This seems a little strange?

It comes down to what happens when you click Render. A brand new session of R is started in the background (you don’t see this), it produces the document, and then closes down.

When we click the green arrow on the task4 R code chunk we are not knitting. We are running code in our current R session. If you never ran the task3 code chunk in our current R session the required variables do not exist yet and cannot be used. So we just need to run the task3 code chunk before running the task4 chunk.

Stop for a second and imagine if the task4 chunk depended on several code chunks. We can run all previous code chunks by clicking the icon with a down arrow and a green bar — do this for the task4 R code chunk. If you check your environment pane (top-right), then you will see the needed pieces have been created. Now click the green arrow on the task4 chunk and it should return 405 in the console.

Task 6

Place a new section right before the Running Code with the header Loading Packages. Create an R code chunk named load-pkgs in the section and use it to load nycflights13, dplyr and knitr.

Now alter the R code chunk below to get it to evaluate/run.

```{r}
#| label: kable-test
#| eval: true
#| message: false

airlines
kable(airlines)
```
# A tibble: 16 x 2
   carrier name                       
   <chr>   <chr>                      
 1 9E      Endeavor Air Inc.          
 2 AA      American Airlines Inc.     
 3 AS      Alaska Airlines Inc.       
 4 B6      JetBlue Airways            
 5 DL      Delta Air Lines Inc.       
 6 EV      ExpressJet Airlines Inc.   
 7 F9      Frontier Airlines Inc.     
 8 FL      AirTran Airways Corporation
 9 HA      Hawaiian Airlines Inc.     
10 MQ      Envoy Air                  
11 OO      SkyWest Airlines Inc.      
12 UA      United Air Lines Inc.      
13 US      US Airways Inc.            
14 VX      Virgin America             
15 WN      Southwest Airlines Co.     
16 YV      Mesa Airlines Inc.         
carrier name
9E Endeavor Air Inc.
AA American Airlines Inc.
AS Alaska Airlines Inc.
B6 JetBlue Airways
DL Delta Air Lines Inc.
EV ExpressJet Airlines Inc.
F9 Frontier Airlines Inc.
FL AirTran Airways Corporation
HA Hawaiian Airlines Inc.
MQ Envoy Air
OO SkyWest Airlines Inc.
UA United Air Lines Inc.
US US Airways Inc.
VX Virgin America
WN Southwest Airlines Co.
YV Mesa Airlines Inc.

Optional challenge (don’t have to complete)

Alter the R code chunk options for load-pkgs to suppress the messages from appearing.

Task 7

Export your completed document and submit the activity on Canvas.

  1. Be sure you always render the final document before you export.
  2. It will create a document in the Files pane called Last_First_act_01.html. Check/click the box next to this file.
  3. In the Files pane navigate to the gear sprocket. And click the Export... option.
  4. Once your .html is downloaded. Look at it BEFORE submitting! Make sure everything is completed.
  5. In Canvas click on the Assignment Activity 01 and upload the html you downloaded.