You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/14-reproducible-documents.Rmd
+43-12Lines changed: 43 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: Producing Reports With knitr
2
+
title: Producing Reproducible Reports
3
3
teaching: 60
4
4
exercises: 15
5
5
source: Rmd
@@ -44,8 +44,8 @@ game of fixing new mistakes resulting from a single formatting change.
44
44
Creating a report as a web page (which is an html file) using R Markdown makes things easier.
45
45
The report can be one long stream, so tall figures that wouldn't ordinarily fit on
46
46
one page can be kept at full size and easier to read, since the reader can simply
47
-
keep scrolling. Additionally, the formatting of and R Markdown document is simple and easy to modify, allowing you to spend
48
-
more time on your analyses instead of writing reports.
47
+
keep scrolling. Additionally, the formatting of and R Markdown document is simple and easy to modify,
48
+
allowing you to spend more time on your analyses instead of writing reports.
49
49
50
50
## Literate programming
51
51
@@ -62,9 +62,12 @@ be executed, and graphs or other results will be inserted into the final documen
62
62
63
63
This sort of idea has been called "literate programming".
64
64
65
-
`knitr` allows you to mix basically any type of text with code from different programming languages, but we recommend that you use `R Markdown`, which mixes Markdown
66
-
with R. [Markdown](https://www.markdownguide.org/) is a light-weight mark-up language for creating web
67
-
pages.
65
+
`knitr` allows you to mix text with code from different programming languages,
66
+
but we recommend that you use `R Markdown`, which mixes Markdown with R.
67
+
[Markdown](https://www.markdownguide.org/) is a light-weight mark-up language for creating web pages.
68
+
69
+
You can either create `R Markdown` files directly or use
70
+
[`quarto`](https://quarto.org/docs/computations/r.html) to create your documents.
68
71
69
72
## Creating an R Markdown file
70
73
@@ -75,10 +78,25 @@ you'll get a dialog box like this:
75
78
76
79
You can stick with the default (HTML output), but give it a title.
77
80
81
+
## Creating a Quarto Document
82
+
83
+
Within RStudio, click File > New File > Quarto Document... and
84
+
you'll get a dialog box like this:
85
+
86
+
{alt='Screenshot of the New Quarto Document dialogue box in RStudio'}
87
+
88
+
You can stick with the default (HTML output), but give it a title. Be
89
+
sure to uncheck the visual editor checkbox and choose the Knitr engine.
90
+
78
91
## Basic components of R Markdown
79
92
80
-
The initial chunk of text (header) contains instructions for R to specify what kind of document will be created, and the options chosen. You can use the header to give your document a title, author, date, and tell it what type of output you want
81
-
to produce. In this case, we're creating an html document.
93
+
The initial chunk of text (header) contains instructions for R to specify
94
+
what kind of document will be created, and the options chosen. You can use
95
+
the header to give your document a title, author, date, and tell it what
96
+
type of output you want to produce. In this case, we're creating an html
97
+
document.
98
+
99
+
For an R Markdown document, you should get:
82
100
83
101
```
84
102
---
@@ -89,6 +107,16 @@ output: html_document
89
107
---
90
108
```
91
109
110
+
For a Quarto document, you should get:
111
+
112
+
```
113
+
---
114
+
title: "Initial Quarto Document"
115
+
author: "Karl Broman"
116
+
format: html
117
+
---
118
+
```
119
+
92
120
You can delete any of those fields if you don't want them
93
121
included. The double-quotes aren't strictly *necessary* in this case.
94
122
They're mostly needed if you want to include a colon in the title.
@@ -181,7 +209,7 @@ the "Knit" button in the upper-left.
181
209
182
210
## Challenge 1
183
211
184
-
Create a new R Markdown document. Delete all of the R code chunks
212
+
Create a new R Markdown or Quarto document. Delete all of the R code chunks
185
213
and write a bit of Markdown (some sections, some italicized
186
214
text, and an itemized list).
187
215
@@ -191,7 +219,8 @@ Convert the document to a webpage.
191
219
192
220
## Solution to Challenge 1
193
221
194
-
In RStudio, select File > New file > R Markdown...
222
+
For R Markdown in RStudio, select File > New file > R Markdown...
223
+
and for Quarto in Rstudio, select File > New File > Quarto Document...
195
224
196
225
Delete the placeholder text and add the following:
197
226
@@ -303,7 +332,7 @@ plot(lifeExp ~ year, data = gapminder)
303
332
304
333
## How things get compiled
305
334
306
-
When you press the "Knit" button, the R Markdown document is
335
+
When you press the "Knit" button, the R Markdown or Quarto document is
307
336
processed by [`knitr`](https://yihui.name/knitr) and a plain Markdown
308
337
document is produced (as well as, potentially, a set of figure files): the R code is executed
309
338
and replaced by both the input and the output; if figures are
@@ -480,7 +509,9 @@ This mode also includes a toolbar at the top with basic formatting buttons,
480
509
similar to what you might see in common word processing software programs.
481
510
You can turn visual editing on and off by pressing
482
511
the {alt='Icon for turning on and off the visual editing mode in RStudio, which looks like a pair of compasses'}
483
-
button in the top right corner of your R Markdown document.
512
+
button in the top right corner of your R Markdown document or switching between
513
+
the source and visual modes {alt='Buttons for changing between source and visual editing modes in RStudio'}
0 commit comments