R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Adding Code Chunk

An R Markdown Chunk is a section you embed in your code that allows you to run R code.

Exercise 1: Insert an R code chunk on a new line below. After the Solution 1: text below, add a statement to the fact that you completed the above exercise successfully or mention any difficulties you encountered or help received to complete this exercise.

Solution 1:

I am able to add the code chunk.

Run Code

Exercise 2: Try some basic math problems in the code chunk below and see how R works as a calculator.

Please recall the order of operations and how that may affect your answer. For example, try calculating 4 + 7 / 10 ^ 2. What happens if you put parentheses around 4 + 7?

Solution 2:

4+7/10^2
## [1] 4.07
(4+7)/10^2
## [1] 0.11

Note: basicmath is called the label of the code chunk.

Exercise 3: Use the right facing arrow to run the code chunk above labeled basicmath and observe the output of running the code chunk. After the Solution 3: text below, add a statement to the fact that you completed the above exercise successfully or mention any difficulties you encountered or help received to complete this exercise.

Solution 3: [1] 4.07 [1] 0.11 I completed the above exercise.

Extra : To be completed during first discussion section

Keep reading for some more Rmarkdown basics.

Adding echo parameter to a code chunk

Using the echo = FALSE parameter to the code chunk prevents printing of the R code that generated the plot.

Exercise 4: Knit the document and check that the code chunk labeled pressure did not print. After the Solution 4: text below, add a statement so that we can verify that you completed the above exercise successfully or mention any difficulties you encountered or help received to complete this exercise.

Solution 4: I have completed the above exercise and the plot is printed without the code chunk.

Exercise 5: Copy the pressure code chunk, label it as pressure2, change the echo parameter to echo = TRUE. Knit the document and check that the code chunk printed along with the plot..

Add text Solution 5: below this paragraph. Next add your code chunk below the text Solution :. Below the code chunk include a statement so that we can verify that you completed the above exercise successfully or mention any difficulties you encountered or help received to complete this exercise.

Solution 5: I have completed the above exercise and now both the plot and the code chunk are printed.

plot(pressure)

Note:

  1. If you forget to give a code chunk a unique label, it won’t knit. You’ll get an error Duplicate chunk label 'pressure'.
  2. If instead of labeling the code chunk pressure2, you accidentally change the code to plot(pressure2), you’ll get an error Error in plot(pressure2) : object 'pressure2' not found.

Can you fix these errors? If not, ask for help from your peers, ULA, TA, instructor.

From now on, you should make it a point to add the text Solution x: and add your solutions below that text whenever you begin solving an exercise in class, discussion or homework and also summarize your findings or any help received from doing the exercise whether or not we explicitly ask you to document it.

R Markdown YAML

You will notice that at the very top of this file, there is a header with several lines of code, with three dashes above it and three dashes below. This is called the YAML of the Rmarkdown (.Rmd) file

If you are curious, google what is YAML to find it’s full form.

---
title: "Rmd basics"
author: "Your name here"
date: "2025-01-09"
output: 
  html_document: 
    toc: yes
    toc_float: yes
---

The YAML may include the title, sub-title, the author name, the date, and the output (among other things).The output tells R what kind of output you would like to make (e.g., a html document or Word document or a pdf). You can change this if you would like and even if it says html, it’s possible to override it to knit to Word or pdf even without changing the header.

Exercise 6: What does YAML stand for?

Solution 6: It stands for “Yet Another Markup Language” or “YAML Ain’t Markup Language”.

Headings and subheadings

Outside of each chunk, if you type text, it will appear in your final report. To denote when you are moving from one section (text+ any R Chunk related to that text) to another, you should making a heading for that section. To make a heading, you use the # symbol. If you want it to be a large heading, use only one or two #. If you want to make a subheading, use more of them. By creating a heading, you are creating a section of text + any R chunk code or plots.

Observe how headers and subheadings have been used in this document and how they appear in the knitted document.

Exercise 7: What did you observe about how headers and subheadings have been used in this document and how they appear in the knitted document?

Solution 7:

Headers are used to indicate the overall subject matter discussed below;

Subheadings are used to elaborate on topics under the subject matter.

Inline code

Let’s say that you want to show your reader what some code would look like, but don’t want to actually execute it with a chunk. You can use a back quote (i.e., `) before and after an area of text to create a gray box similar to that which displays the text of a chunk.

For example: 4+4 equals 8 was created by typing ` 4+4 equals 8 `.

Simple one line math operations can be executed without a chunk by using typing r 4+4 within two back tics `. Check that typing r 4+4 within two back tics actually evaluates and prints out on the result 8 rather than printing out the operation 4+4: 8

Exercise 8: What is 12 x 13? Show both the code that should be executed as well as result of its execution.

Solution 8:

Code to be executed: ` r 12*13 `

Result: 156

Table

Another thing you can do in the text of a R Markdown file is create a table. Below is the code I used to create a table and the output when I put the code inside my Markdown file.

Column 1 | Column 2
-------- | --------
Cell 1   | Cell 2
Cell 3   | Cell 4 
Column 1 Column 2
Cell 1 Cell 2
Cell 3 Cell 4

Exercise 9: Tabulate the days of the week and number of hours of classes you take each day along with the weekly total hours you spend in class this quarter.

Solution 9:

Weekday Hours
Monday 0
Tuesday 4.75
Wednesday 1.25
Thursday 4.75
Friday 2.25
Saturday 0
Sunday 0
Total 13.0

Formating text bold or italics

In Markdown, if you would like your text to be in italics or bold, you should (respectively) put one or two asterisks both before and after the section you want to appear that way. The below table demonstrates what you should type in Markdown and how it then appears.

Markdown format Output
*italics* italics
**bold** bold

Exercise 10: Write a sentence about your favorite color. Make sure the color appears in bold or italics.

Solution 10: My favourite colour is the Dutch Crimson(#AD1D25), my former favourite colour is Crimson(#990000).

A final note on knitting

Sometimes your file won’t be able to knit. If there is an issue with your code in one (or more) of the chunks, then R won’t be able to knit the Markdown file. Helpfully, R will tell you where (line number where) the first error in the document occurred should there be an issue. If your Markdown file still won’t knit, another thing to double check is to make sure that you haven’t given two chunks the same name.

Tip

Keep knitting your file every time you add something - a code chunk or a paragraph of text or a plot. If you wait to knit till you’ve added everything, resolving the errors will get frustrating.

Exercise 12: Check that

  1. You are working in the workingfiles folder and not the content folder
  2. you have completed all exercises in the worksheet .Rmd file
  3. Each of your solutions appears below the text like Solution x:
  4. that you can knit your worksheet into html
  5. You have changed the author name in YAML to your information.
  6. (Optional for this worksheet, necessary for future assignments:) We appreciate if you change the file name to Worksheet01_yournetid.Rmd by going to files pane, selecting the worksheet and then using the Rename button. Knit this document again to get the html for submission. In future, before starting to work on the .Rmd file, it’ll be better to rename the file with your netid before editing it.
  7. You will download your knitted html document and a .zip file, containing: your .html file, your .Rmd file, and any other relevant files and upload it to Canvas Homework 01 submission. Please rename your completed files to Homework01_yournetid.extension before uploading to canvas where .extension could be .Rmd, .html, .zip etc.
  8. Do not forget to complete writing down your learning gains below.

Solution 12:

I have checked and completed step 1 through step 8.

Note : Please stop by office hours if you are struggling in the course ever. This will help ensure that you don’t fall behind. ULA’s and Tas along with me, your instructor will be a great resource to aid your learning in the course. Even if you do not need help with this worksheet, consider stopping by to say hello to a ULA, TA or myself and asking them something about the course or PSTAT department in general or even ask them how they can help you in the course!

Exercise 13:Learning Gains today:

Solution 13:

What did you learn today?

I have learned markdown before, so I find many striking similarities in rmarkdown.

  • Topic 1: Now I know to use three back ticks and curved brackets with declaration and parameters to create code chunks.
  • Topic 2: Now I have learned how to write inline code with convenience.
  • Topic 3: I have learned to use echo as a parameter to show the code that actualizes the website.