R Markdown for FW 536 — install, knit, and submit
Every assignment in this course is submitted as an R Markdown HTML output. This 30-minute tutorial walks you through installation, creating your first document, embedding code, adding photos of hand-written work, and uploading to Canvas. Please complete the install steps before Day 1.
Why R Markdown
R Markdown lets you write a single document that contains:
- Narrative prose explaining what you did and why.
- Live R code that runs and produces output (numbers, plots, tables).
- Math equations rendered nicely.
- Photos of hand-written work for problems best done on paper.
When you "knit" the document, R Markdown runs all the code, captures the output, and produces a polished HTML file you can submit. The grader sees both your reasoning and your code in one place — which is exactly how scientific writing should work.
Install — do this BEFORE Day 1
1. R (the language)
Download from cran.r-project.org. Pick your platform; install the most recent stable release.
2. RStudio (the editor)
Download the free RStudio Desktop from posit.co/download/rstudio-desktop. Install after R.
3. R Markdown and knitr packages
Open RStudio and run, in the Console pane:
install.packages(c("rmarkdown", "knitr"))
RStudio may also prompt you to install these the first time you open a new R Markdown file — accept the prompt.
4. (Optional but recommended) Pandoc
RStudio ships with a bundled Pandoc, so you usually don't need to install it separately. If knitting fails with a "pandoc not found" error, install it from pandoc.org/installing.html.
xcode-select --install in the Terminal).Windows users: install Rtools from cran.r-project.org/bin/windows/Rtools.
Your first R Markdown document
- In RStudio: File → New File → R Markdown…
- Choose HTML as the output format. Give the document a title (e.g., "FW536 — Day 1 morning lab — YourName").
- RStudio creates a starter
.Rmdwith example content. - Click the Knit button (a yarn-and-needles icon at the top of the editor).
- RStudio renders the document and opens it in a viewer pane and as a
.htmlfile in your working directory.
If this works, you're ready. The rest of this tutorial covers the parts of R Markdown you'll use most often.
Code chunks
An R Markdown document is text with embedded code "chunks." A chunk looks like:
```{r}
x <- rnorm(100)
mean(x)
hist(x)
```
Everything inside the triple-backtick block is R code; it will be executed when you knit, and the output (numbers, plots) is inserted into the HTML in place.
Chunk options
You can add options inside the curly braces to control how the chunk behaves:
| Option | Does | Example |
|---|---|---|
echo = FALSE | Hide the code; show only the output. | {r, echo=FALSE} |
eval = FALSE | Show the code but don't run it. | {r, eval=FALSE} |
message = FALSE | Suppress package start-up messages. | {r, message=FALSE} |
warning = FALSE | Suppress warnings. | {r, warning=FALSE} |
fig.width = 6, fig.height = 4 | Plot dimensions (inches). | {r, fig.width=6} |
cache = TRUE | Cache the chunk so re-knits skip slow code. | {r, cache=TRUE} |
The most useful default for FW 536 assignments: leave echo ON so graders can see your code; turn message and warning OFF for clean output.
Markdown text
Outside of chunks, you use Markdown to format prose. The most useful patterns:
| Markdown | Rendered |
|---|---|
# Header 1 | large heading |
## Header 2 | medium heading |
### Header 3 | smaller heading |
**bold** | bold |
*italic* | italic |
`inline code` | inline code |
- item or * item | bullet list (one per line) |
1. item | numbered list |
[link text](https://url) | hyperlink |
Math
R Markdown HTML supports LaTeX-style math via MathJax (included automatically).
- Inline math: wrap with single dollar signs.
$\mu = \beta_0 + \beta_1 x$→ $\mu = \beta_0 + \beta_1 x$. - Display math (centered, own line): wrap with double dollar signs.
$$ P(\theta \mid y) = \frac{P(y \mid \theta) P(\theta)}{P(y)} $$
Useful symbols: \alpha, \beta, \theta, \mu, \sigma, \lambda, \sum, \int, \frac{a}{b}, \sqrt{x}, x^2, x_i, \hat{\beta}, \bar{y}, \to, \ge, \le, \sim.
For most FW 536 derivations, math will be cleaner in R Markdown than the equivalent typed-up Word file. But many derivations are also fine to do by hand and photograph — see the next section.
Adding photos of hand-written work
For probability derivations, Punnett squares, or any work that's faster on paper, you can photograph the page (or scan it) and embed the image in R Markdown.
Step 1 — get the photo onto your computer
- Phone camera → AirDrop / Google Drive / email to yourself.
- Or use a phone scanner app (Adobe Scan, CamScanner, Apple Notes) that exports a clean PDF or PNG.
- Crop and rotate if needed.
Step 2 — save the image alongside your .Rmd file
Create a folder named images/ in the same directory as your .Rmd, and drop the photo in there. Suggested naming: problem3_derivation.jpg.
Step 3 — insert the image in R Markdown
Two equally good ways:
Option A — Markdown syntax

Option B — knitr code chunk (gives more control)
```{r, echo=FALSE, out.width="60%", fig.cap="Hand-derivation of Problem 3"}
knitr::include_graphics("images/problem3_derivation.jpg")
```
Step 4 — make sure the image fits
If the photo is huge, restrict width with out.width="60%" (a percentage of the page width) or out.width="400px" (fixed pixels).
.html file to Canvas, the image will appear broken. Two fixes:
- Better: add
self_contained: truein your YAML header (see next section). The HTML will then embed the image as base64 — one file is all you need to upload. - Alternative: upload the
.htmlfile AND theimages/folder together (zip them first).
The YAML header
Every R Markdown file starts with a YAML block between two --- lines. A good default for FW 536:
---
title: "FW 536 — Day 2 afternoon lab — YourName"
author: "Your Name"
date: "`r Sys.Date()`"
output:
html_document:
self_contained: true
toc: true
toc_depth: 3
toc_float: true
code_folding: show
theme: cosmo
---
self_contained: truebundles images and CSS into the HTML so you upload one file.toc: truegenerates a clickable table of contents.toc_float: truefloats the TOC on the side for easy navigation.code_folding: showlets the grader collapse code chunks they don't want to read in detail.theme: cosmopicks a clean visual theme.
Knitting to HTML
Press the Knit button (or use the keyboard shortcut Ctrl/Cmd-Shift-K). RStudio will:
- Run every R code chunk in order.
- Collect outputs (text, numbers, plots).
- Render the prose, math, and images.
- Save a
.htmlfile in the same folder as your.Rmd.
If a chunk has an error, knitting stops at that chunk. Read the error message, fix the code, and re-knit.
Submitting to Canvas
- Knit your
.Rmdto HTML (withself_contained: truein the YAML). - Find the resulting
.htmlfile in your working directory. - Open the Canvas assignment for the lab.
- Upload the
.htmlfile. (If you also want to upload the.Rmdsource, that's welcomed — it helps the grader help you on partial credit.) - Submit before the deadline.
.Rmd alone — the grader can't be sure your code runs cleanly without your local environment. The .html is the deliverable; the .Rmd is a courtesy.
Troubleshooting common errors
| Error | Likely cause | Fix |
|---|---|---|
| "could not find function …" | You forgot to load a package or to install it. | Add library(packageName) at the top of your file; install if needed. |
| "object 'x' not found" | The code chunk depends on something defined in an earlier chunk that didn't run when you knitted. | Restart R, then Knit. Make sure every chunk you depend on is defined in order in the document. |
| "pandoc not found" | RStudio's bundled Pandoc isn't on your PATH. | Install Pandoc separately, or update RStudio. |
| Image shows as a broken icon in the HTML | HTML is referencing the image path; the images/ folder didn't go to Canvas. | Add self_contained: true to YAML and re-knit; the image will be embedded. |
| Plot is too small/large | Default chunk size doesn't match what you want. | Use chunk options fig.width=6, fig.height=4 or out.width="60%". |
| "$ outside math mode" | Stray dollar sign in plain prose is being interpreted as math. | Escape it: \$ to print a literal dollar sign. |
A working template
Download rmarkdown_tutorial_starter.Rmd for a known-good starter file. Open it in RStudio, knit it once to confirm everything works, then save a copy and re-use it for every FW 536 lab.
Per-lab R Markdown templates are also provided alongside each problem set in the day's folder (e.g., Day1_Probability/morning_lab_template.Rmd). Start from those — they pre-set the right title and chunks so you can focus on the content.