-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquarto.qmd
287 lines (195 loc) · 6.8 KB
/
quarto.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
title: 'PHS 7045 Advanced Programming<br>Example with Quarto'
author: <a href="https://ggvy.cl"><b>George G. Vega Yon, Ph.D.</b></a><br>george.vegayon@utah.edu<br><br>University of Utah
date: January 12, 2023
format:
html:
embed-resources: true
code-fold: show
toc: true
---
## Quarto files
```{r setup-chunk}
#| echo: false
#| warning: false
#| message: false
library(ggplot2)
library(dplyr)
library(gapminder)
library(knitr)
opts_chunk$set(warning = FALSE, message = FALSE, echo = FALSE, comment = "")
```
* These are plain-text (not binary) files
```{r hello-rmd}
cat(readLines("hello-world.qmd"), sep="\n")
```
## Main components of a qmd file
::: {.r-fit-text}
* The header: Information about the document in [yaml](https://en.wikipedia.org/wiki/YAML){target="_blank"} format
```{r hello-rmd-yaml}
cat(readLines("hello-world.qmd")[1:6], sep="\n")
```
* R code chunks (with options)
```{r hello-rmd-chunk1}
cat(readLines("hello-world.qmd")[16:18], sep="\n")
```
* R code chunks (without options)
```{r hello-rmd-chunk2}
cat(readLines("hello-world.qmd")[24:27], sep="\n")
```
:::
---
* Some other options include:
- `cache`: Logical, when `true` saves the result of the code chunk so it
doesn't need to compute it every time (handy for time-consuming code!)
- `messages`: Logical, when `true` it suppresses whatever message the R
code in the chunk generates.
- `fig.cap`: Character vector. Specifies the title of plots generated
within the chunk.
More [here](https://yihui.name/knitr/options/#chunk_options).
## How it works
::: {.r-fit-text}
::: {.fragment}
{style="width: 800px;"}
:::
Source: Quarto website https://quarto.org/docs/faq/rmarkdown.html
* The function `quarto` passes the qmd file to [**knitr**](https://cran.r-project.org/package=knitr)
* knitr executes the R code (or whatever code is there) and creates an `md` file
(markdown, not Rmarkdown)
* Then the `md` file is passed to [**pandoc**](http://pandoc.org/),
which ultimately compiles the
document in the desired format as specified in the `output` option
of the header.
:::
<!-- # Questions -->
<!-- Taking a look at the Rmarkdown cheatsheet, answer the following questions: -->
<!-- 1. How can you write bold faced text? What about italics? -->
<!-- 2. Include a 1st, 2nd, and 3rd level headers in a document? -->
<!-- 3. Include an R code **chunk** without it being evaluated? -->
<!-- 4. Tell pandoc to compile a MS Word document? -->
<!-- 5. Include a figure from a `png` file that you have in the same folder as your -->
<!-- markdown document? -->
<!-- 6. Include a mathematical equation? -->
## Quarto supports other formats
* The following code chunk requires having the [**reticulate**](https://cran.r-project.org/package=reticulate) R package (R interface to Python)
```{r}
#| label: pypy
cat("```{py some-py-code}\nprint \"Hello World\"\nimport this\n```\n")
```
```{python}
#| label: some-py-code
#| echo: false
print("Hello World")
import this
```
## Tables with Quarto
* Suppose that we want to include the following data as a table part of our
document
```{r}
#| echo: true
#| label: stats-by-year
# Loading the package
library(gapminder)
# Calculating stats at the year level
stats_by_year <- gapminder %>%
group_by(year) %>%
summarise(
`Life Expectancy` = mean(lifeExp),
`Population` = mean(pop),
`GDP pp` = mean(gdpPercap)
) %>%
arrange(year)
stats_by_year
```
There are at least two ways of doing it
### Tabulation with `knitr`
::: {.r-fit-text}
* The knitr package provides the function `kable` to print tables.
* It has the nice feature that you don't need to be explicit about the format,
i.e., it will automatically guess what type of document you are working with.
```{r}
#| echo: true
#| label: kable
knitr::kable(
head(stats_by_year),
caption = "Year stats from the gapminder data",
format.args = list(big.mark=",")
)
```
* Checkout [**kableExtra**](https://cran.r-project.org/package=kableExtra) which
provides extensions to the `kable` function.
:::
### Tabulation with `pander`
::: {.r-fit-text}
* Another (very cool) R package is [**pander**](https://cran.r-project.org/package=pander)
* It provides helper functions to work with pandoc's markdown format
* This means that you don't need to think about what is the final output
format
```{r}
#| echo: true
#| label: pandoc
#| results: 'asis'
pander::pandoc.table(
head(stats_by_year),
caption = "Year stats from the gapminder data"
)
```
:::
## Regression tables
::: {.r-fit-text}
* There are a lot of functions around to include regression output
* Suppose that we run the following models on the `diamonds` dataset
```{r}
#| warning: false
#| message: false
#| label: multiple-regressions
#| echo: true
data(diamonds, package="ggplot2")
# Model 1
model1 <- lm(price ~ carat, data = diamonds)
model2 <- lm(price ~ carat + depth, data = diamonds)
model3 <- lm(price ~ carat + table, data = diamonds)
model4 <- lm(price ~ carat + depth + table, data = diamonds)
# Let's put it all in a list to handle it together
models <- list(model1, model2, model3, model4)
```
* How can we include these in our report/paper?
:::
### Regression tables with `texreg`
::: {.r-fit-text}
* The R package [**texreg**](https://cran.r-project.org/package=texreg){target="_blank"}
```{r}
#| results: asis
#| label: texreg
#| echo: true
texreg::htmlreg(models, doctype=FALSE)
```
* It also has the functions `texreg`, for LaTeX tables, and `screenreg`, for plaintext output
* The problem, you have to be explicit in the type of table that you want to print
:::
### Regression tables with `memisc`
::: {.r-fit-text}
* The R package [**memisc**](https://cran.r-project.org/package=memisc){target="_blank"}
```{r}
#| results: asis
#| echo: true
#| label: memisc
library(memisc)
tab <- mtable(
`Model 1` = model1,
`Model 2` = model2,
`Model 3` = model3,
`Model 4` = model4,
summary.stats=c("sigma","R-squared","F","p","N")
) %>% write.mtable(file = stdout(), format = "HTML")
```
:::
## Plots with Quarto
* In the case of plots, these just work!
```{r}
#| echo: true
#| label: plot
ggplot(diamonds, aes(x = carat, y = price, color=cut)) +
geom_point() +
ggtitle("Plots with Quarto just work")
```