.html
file
and a a .zip
file, containing: your .html
file, your .Rmd
file, and any other relevant files to
Canvas using the submission link for homework 01.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.
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.
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.
Keep reading for some more Rmarkdown basics.
echo
parameter to a code chunkUsing 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:
Duplicate chunk label 'pressure'
.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.
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”.
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:
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
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 |
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).
Finally, if you want to include a hyperlink in your Markdown file, do
it by putting the word you want to have the link in brackets [] and the
website in (). For example, if I wanted to link to the RStudio website,
I would do it like this [This](https://rstudio.com/)
This is what that code creates
outside of the quotes I used to show the example. Clicking on the word
This
in the output file takes you to the hyperlink.
Note: There can be no space between the square and
round brackets ](
otherwise you’ll get an error or the link
won’t show in your knitted document.
Exercise 11: Add a hyperlink to PSTAT 10 canvas course.
Solution 11: PSTAT10 Canvas Instructure
There are many other things that you can do in Markdown beyond these those discussed here; a helpful cheat sheet is available here.
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.
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
workingfiles
folder and not the
content
folder.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.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!
Solution 13:
What did you learn today?
I have learned markdown before, so I find many striking similarities in rmarkdown.