5  Start a Report

How to make a Quarto markdown (.qmd) file for your labs

Author

Shane McCarty

Published

10.05.2026

Abstract

This chapter is the minimum a researcher needs to start a lab file in RStudio: make a Quarto markdown (.qmd) document, name it, give it a short YAML header, add the chunk that loads the packages, write a few words, and click Render. It explains what a code chunk is and the one chunk option every chunk gets (a label). The full requirements for the RD Report and Final Report, from the complete YAML header to the five required parts of every chunk, wait until Lab 6, in Start My Report.

Keywords

quarto, YAML, code chunks, render

GoGo: this chapter is for your lab files

You need a .qmd file for every lab from Lab 2 on. This chapter gives you just enough to make one, write in it, run code in it, and render it. Your RD Report and Final Report have more rules (a full YAML header, five required parts in every chunk); those are in Start My Report, the first chapter of Lab 6, and you do not need them yet.

In RStudio, you should see the Source and Visual editor options in the top left corner of the script editor. You can toggle back and forth between the two options.

This playbook shows code in Source view, so if a screenshot looks different from your screen, check which button is selected.

5.1 What a Quarto Document Is

A traditional R script (.R) involves code throughout the entire script. A Quarto Markdown Document (.qmd) integrates the R programming language with markdown language into a single document that can include a mix of code and written content. We use report.qmd and other .qmd files to write a reproducible manuscript using written text within embedded code chunks.

You have three places to write code in RStudio: the console (runs now, never saved), an .R script (saved, code only, used here for install.R), and a .qmd document (saved, code and writing together, rendered to a web page). R Software compares them. For your reports, the answer is always the .qmd:

ImportantImportant: Write your analysis in a .qmd file, not in the console

In this course, all of the code for your reports goes in code chunks inside your .qmd file. Here is why.

  • It can be checked. Your peer mentor, Dr. Shane, your teammates, and later readers can open your .qmd and see exactly how every number and plot was made. Code typed in the console leaves no record, so nobody can review it, and neither can you.
  • It can be published. Rendering a .qmd turns your code, results, and writing into one web page with a URL, which is what you submit (Publish). A console session cannot be published.
  • It can be redone. Render runs every chunk from a fresh start, so your report proves that the analysis works from top to bottom. That is what reproducible means, and it is the point of Reproducible Reports.
  • It keeps the explanation next to the code. A .qmd holds your Methods and Results paragraphs beside the code that produced them, so the writing and the numbers cannot drift apart.

Use the console to try things. When a line works and belongs in your analysis, put it in a chunk in your .qmd.

5.2 Create a Quarto Markdown Document

  • File > New File > Quarto Document
  • Complete Title and Author Name, Create
  • File > Save As > practice.qmd (in Lab 1 this is a practice file, so any name works; the lab files get their names below)
  • Check the file shows up in your files in the bottom right window as .qmd file

5.2.1 Name the file

One .qmd per lab, named Lastname_lab2.qmd, Lastname_lab3.qmd, Lastname_lab4.qmd, and Lastname_lab5.qmd, with your own last name in place of Lastname (for example, McCarty_lab2.qmd). There is no Lastname_lab1.qmd: in Lab 1 you only set up the software and practice with a throwaway file, and your first lab file is Lastname_lab2.qmd, which you make at the start of Lab 2. You will not submit these labs. They are practice, so that you are ready when you analyze your team’s research data in your report, which gets its own file name in Lab 6.

CautionCaution: Save your file before you close RStudio

RStudio does not save your .qmd or .R file for you. If the file name on its tab is red with a *, your latest changes exist only on the screen, and closing RStudio throws them away. Two different questions come up when you quit, and they need opposite answers.

Do this

  • Press Cmd + S (Mac) or Ctrl + S (Windows) every few minutes, and always before you Render. The file name on the tab turns black when it is saved.
  • Before you quit, click File > Save All.
  • If RStudio asks “Save changes to report.qmd?”, click Save. That question is about your file.
  • Copy your .qmd and .R files to your ELN at the end of every session (Find the Ref).

Do not do this

  • Do not close RStudio, or your laptop, while a file name is still red.
  • Do not click Don’t Save when the question names a file (report.qmd, install.R). Only the question about the workspace (.RData) gets Don’t Save: the workspace is leftovers, and your file is the recipe.
  • Do not rely on Render to save for you. It usually does, but only for the file you rendered, and only if the render starts.

5.3 YAML: the header of the file

YAML is the block between the two --- lines at the very top of a .qmd file. It holds information about the file (its title, your name, the date, what kind of output to make) rather than the content of the file. RStudio writes a short one for you when you create the document. For a lab file, this is all you need:

---
title: "Lab 2: Clean Data"            # SWAP: the lab
author: "Your Name"                   # SWAP: your name
date: 10.09.2026                      # SWAP: today's date, written MM.DD.YYYY
date-format: "MM.DD.YYYY"
format: html
---

Four things to notice: every line is name: value with one space after the colon; text with spaces goes in quotation marks; format: html means Render makes a web page; and the --- lines are part of it. That is all the YAML there is until Lab 6.

Everything below the closing --- is the body of your report. The YAML is only the header: it sets the title, author, and format, and it appears once, at the very top. Write your report text and add your R code chunks below that line, in the same .qmd file. You do not need a separate file for your code because your code is in the document with the text. This document type is what allows you to publish a Quarto report, such as the zero-sum report.

A Quarto file named Lastname_lab2.qmd shown in an editor with line numbers. Lines 1 to 8, shaded purple and labeled YAML header, hold the title, author, and format between two lines of three dashes. Below them, unshaded lines labeled Your writing hold a heading and a sentence. Gray shaded lines labeled Code chunk hold R code between the fences with curly-brace r and three backticks. Writing and code chunks alternate down to line 31.

The parts of a .qmd file. The YAML header sits between the two --- lines at the top. Below it, your writing and your code chunks take turns, as many times as you need.
CautionCaution: In YAML, spaces are part of the code

YAML reads indentation the way R reads parentheses. Every line’s position matters: title: starts at the left edge, html: sits two spaces in under format:, and toc: true sits two spaces in under html:. Add or remove one space and the render fails, usually with a message that does not mention spacing at all, such as “YAML parse exception” or “did not find expected key.”

Do this

  • Copy the YAML above with the clipboard icon and paste it in. Do not retype it.
  • Change only the words after the colons: the title, your name, the date.
  • Keep one space after every colon (title: "...", not title:"...").
  • Keep the --- lines at the very top of the file, with nothing above the first one.

Do not do this

  • Do not press Tab inside the YAML. Tab characters are not spaces, and YAML rejects them. If you must indent, use the space bar, two spaces at a time.
  • Do not delete a line you do not understand. If you do not want a line, put # in front of it so YAML ignores it.
  • Do not let the visual editor “help”: switch to Source view before you edit the header, so you can see the spaces.

The same rule applies to code chunk options. Every #| line needs a space after the |, and one after the colon: #| label: import-data, not #|label:import-data.

For more information, see:

5.4 Your First Chunk: Load Your Packages

Right under the YAML header, every report starts with one code chunk that loads the packages the report uses. You wrote this chunk in R Packages; copy it into every new .qmd file, starting with Lastname_lab2.qmd. Nothing in the report will run without it, because a fresh R session knows no packages until library() is called.

## IMPORT
library(readxl)    # read in xlsx files

## TIDY + TRANSFORM + VISUALIZE
library(tidyverse) # loads tidyr, dplyr, ggplot2, and more
library(naniar)    # replace -99 with NA
library(psych)     # composite scores, reliability, and descriptive stats

## COMMUNICATE
library(knitr)     # tables

Run it once with the green ▶ arrow when you open the file, and again every time you reopen RStudio. That habit is the first step of Find the Ref, the next chapter.

5.5 Code Chunks

A code chunk is a block of R code inside your writing. It starts with a line of three backticks and {r}, and ends with a line of three backticks. Everything between those lines is R; everything outside them is text. RStudio inserts an empty chunk for you with Cmd + Option + I (Mac) or Ctrl + Alt + I (Windows), or the green +C button at the top of the editor.

```{r}
#| label: import-alldata

alldata <- read_excel("data/SWAPFILE.xlsx")
```

The line that starts with #| is a chunk option: a setting Quarto reads before it runs the chunk. The only option you need for your labs is a label: one short name for the chunk, lowercase, with hyphens between words, that says what the chunk does and to what (load-library, import-alldata, mutate-health-3cat, plot1-wellbeing-treated-barplot). Labels make error messages readable (“Error in import-alldata”) and are required in your report, so start the habit now.

A plain # inside a chunk is a comment: a note for people that R skips. You will see comments in every chunk in this playbook; write your own whenever a line is not obvious. Two comments appear at the bottom of every solution in the Labs, #source: (where the code came from) and #explanation: (what the chunk did, in your own words). They are required in your report and explained in Start My Report; in your lab files they are a good habit, not a rule.

GoGo: #| is an option, # is a comment

They look alike, but they do different jobs. #| at the top of a chunk sets a chunk option that Quarto reads, such as #| label:. A plain # anywhere else is a comment that Quarto and R both ignore.

Run a chunk with the green ▶ arrow at its top right; the output appears right under it. The chapters from Import Data Once on give you the chunks to run, one play at a time.

5.6 Render Your Report

Writing a .qmd is only half of the job. Render turns it into the report. Click the Render button (the blue arrow the Render button, a blue arrow) at the top of the editor. Quarto then does three things:

  1. It starts a fresh R session with nothing in memory.
  2. It runs every code chunk from the first line to the last, in order.
  3. It builds a web page (Lastname_lab2.html, next to your .qmd in the project folder) with your writing, your code, your plots, and your results, and shows it to you.

Where the report appears depends on your RStudio settings. Usually it opens in the Viewer tab (bottom right). It may also open in a separate RStudio window, or in your web browser (Safari, Chrome, Edge). All three show the same page. To see it in your web browser, click the Show in new window icon at the top of the Viewer tab. You can also double-click the .html file in your project folder at any time. It is an ordinary web page, so it opens in your browser even when RStudio is closed.

Because the session is fresh, nothing you ran by hand counts: a package you loaded in the console, an object you made yesterday. If the report needs it, it has to be in a chunk. That is also why a successful render is proof that your analysis works from top to bottom.

The first time you render, RStudio may ask to install packages it needs (such as rmarkdown and knitr). Click Yes.

GoGo: Render early and often

Do not wait until the report is finished to render it for the first time. Render after your load-library chunk, again after your import chunk, and again after each new plot. A render that fails after one new chunk tells you exactly where the problem is. A render that fails after twenty new chunks does not.

If the render stops with an error, the console shows the message. Fix the first error, then render again; and if the message does not say where the problem is, run the chunks one at a time from the top (R Software, Running code). Before you submit, run the full fresh-start check in Find the Ref.