Appendix: Quarto Tutorial

Quarto is a wonderful tool available in the R Studio framework for integrating text, R code, and statistical and graphic output into neat and readable documents. It can be integrated into classrooms, both as a tool for presenting material to students and as a resource for student to use for submitting R homework assignments. Quarto documents can be built using either source code or via the visual editor. This brief tutorial uses the visual editor.

The first thing you need to do is open a Quarto document in R Studio. Go to the + tab in the upper-right corner of the script window and select Quarto document:

When you do this, box will appear giving you a few options. You should provide a title for your document, probably identifying which homework assignment, provide your name, select PDF output (unless you want a word or html document), and click on Create Empty Document.

Quarto Environment

Once you do this, the Quarto editor will appear. It includes both a command (top) and an editing (second) toolbar, and should look like this, except without all of the annotations:

I recommend toggling on the Outline button to remove the outline from the right side and give you a wider screen for editing your document.

When writing text, he default is to use the “Normal” font, but you can click on the headings tab to create differently sized section headings:

Running R code

Click on the +C button in the command toolbar to open a code chunk window in your document, and then enter your code in the window. When you have everything you need, click the arrowhead button on the right to preview the results. This is where you find out if everything looks the way you think it should, or if there are mistakes.

You can then add text below the code window to discuss and interpret the results.

Here is an example, using a bit of code from Chapter 1. You can see that I used to code window to tell R to load the Approve21 data set and get the names of all of the variables. When I clicked the upper-right arrowhead, a preview of the results was added:

Sometimes, you may be interested in including the results of the R commands in your documents without including the the code. To do this, you need to add echo=FALSE as a code execution option:

One thing I recommend is that you suppress the output when attaching libraries or installing packages in your Quarto document. This is because sometimes the messages displayed when libraries are loaded can take up a fair amount of space in your document and can be somewhat inelegant, compared to the rest of the document. This is easy to fix by adding output=FALSE as and option:

Producing the Document

Once you have all of the R code you need, along with whatever text you need to explain and interpret the results, you can produce the PDF document by clicking on the Render tab in the command toolbar. If there are no errors, the document will appear on your screen, and I copy will be saved to your working directory.

I have found that working with Quarto is an iterative process. I produce a document, check to see how it looks, go back and edit it in the Quarto editor, produce another copy, edit it, and so on.

Quarto has many more features than just those discussed here. I encourage you to explore and find ways in which Quarto can help you produce the most functional and attractive documents possible. A good place to start is with the resources provided at quarto.org.