11  Quarto & RMarkdown

In this chapter, we’ll cover the basics of RMarkdown, including the syntax, the available output formats, and some common use cases.

11.1 RMarkdown Syntax

RMarkdown documents are written in a simple markup language that is similar to HTML. The basic syntax consists of a mix of plain text and special formatting symbols, which control how the document is rendered.

11.1.1 Headers

RMarkdown headers are used to define the title of the document and its sections. Headers are created using the pound symbol (#), with one pound symbol indicating the main title, two pound symbols indicating a second-level header, and so on. For example:

Code
# My RMarkdown Document
## Section One
### Subsection One

11.1.2 Text Formatting

RMarkdown provides a variety of ways to format text. For example, you can create italicized text using asterisks or underscores, like so:

Code
*This text is italicized*
_This text is also italicized_

Similarly, you can create bold text using double asterisks or double underscores, like so:

Code
**This text is bold**
__This text is also bold__

You can also create lists using either hyphens or asterisks, like so:

Code
- Item 1
- Item 2
- Item 3

or

Code
* Item 1
* Item 2
* Item 3

11.2 Code Blocks

Code blocks are one of the most important features of RMarkdown. For example:

Code
x <- 1:10
mean(x)
Listing 11.1: In this code block, we use the glimpse() function to display the structure of the DEMO dataset. We use the capture.output() function to capture the output of glimpse() and store it in the res variable. Finally, we use the cat() function to print the first 10 lines of the output. The echo = FALSE option is used to prevent the code block from being displayed in the final document.
```{r}
#| echo: false
res <- capture.output(glimpse(DEMO))
cat(c(res[1:10],"…", "…", "<snip>\n"), sep = "\n")
```

11.2.1 Output Formats

RMarkdown allows you to create a wide range of document formats, including HTML, PDF, Microsoft Word, and more. To specify the output format, you simply include a YAML header at the beginning of the document, like so:

Code
title: "My RMarkdown Document"
output: html_document

This YAML header specifies that the document should be rendered as an HTML document. Other output formats include pdf_document, word_document, and beamer_presentation, among others.

11.2.2 Knitting

Once you’ve written an RMarkdown document, you can “knit” it to produce the final output. Knitting is the process of rendering the RMarkdown document to its specified output format. To knit a document, simply click the “Knit” button in the RStudio IDE, or run the render() function in R.

11.3 Common Use Cases

RMarkdown is a versatile tool that can be used for a wide range of purposes. Some common use cases include:

  • Data analysis reports
  • Academic articles
  • Blog posts
  • Tutorial documents
  • Technical documentation

In each of these use cases, RMarkdown provides a way to integrate R code with text, tables, and figures, making it easy to produce dynamic, reproducible documents.

In summary, RMarkdown is a powerful tool for creating dynamic, reproducible documents that integrate R code with text, tables, and figures. With its simple markup language and support for a wide range of output formats, RMarkdown is an essential tool for anyone working with R.