-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmarkdown.qmd
132 lines (84 loc) · 4.35 KB
/
rmarkdown.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
---
title: "Using R Markdown"
author: "R Félix"
eval: false
---
## What is R Markdown
R Markdown is a file format for creating dynamic documents.
It is an R extension of the popular Markdown format, commonly used for writing and formatting text on the web, and incorporates **R** and **Python** code for statistical analysis, visualizations, and data manipulation.
The Rmarkdown document contains the **text**, the **code** and the **results** (figures, tables, or inline calculations), ensuring the **analysis can be reproduced**.
It allows to produce different output formats from a single file to **one or more** formats:
- **HTML**: Interactive and shareable reports.
- **PDF**: Print-ready documents, using LaTeX.
- **Word**: Editable reports `.docx`.
- **Slides**: Presentation-ready content using frameworks like `xaringan` or `ioslides`.
## How it works
When you run `render`, R Markdown feeds the .Rmd file to [knitr](http://yihui.name/knitr/), which executes all of the code chunks and creates a new markdown (.md) document which includes the code and its output.
The markdown file generated by knitr is then processed by [pandoc](http://pandoc.org/) which is responsible for creating the finished format.
![Source: RStudio, PBC](images/clipboard-3350666123.png)
This may sound complicated, but R Markdown makes it extremely simple by encapsulating all of the above processing into a single `render` function.
::: {.callout-tip appearance="simple"}
See more at: <https://rmarkdown.rstudio.com/lesson-1.html> or @xie2018r .
:::
## Example
1. Create an `.Rmd` file in RStudio, and use HTML as output.
![](images/clipboard-1842640045.png){width="360"}
![](images/clipboard-2137208684.png){width="413"}
2. Have a look at the template example and leave it as it is.
3. Save your new `.Rmd`
4. Knit the document (click "Knit" in RStudio) to generate an output in the desired format.
Now, change it to pdf_document in the yaml
``` yaml
output: pdf_document
```
::: {.callout-warning appearance="simple"}
It may take a while for the first time in your computer, due to the `tlmgr` packages it needs.\
The next times will be smoother!
:::
Open the output pdf.
## Code Chunk
To load libraries, load your data, make your analysis, plot your results, you will need to include code chunks along your document.
You can also use the `Ctrl`+`Alt`+`i` shortcut.
![](images/clipboard-3320452517.png){fig-align="center" width="353"}
A code chunk in R will show up
![](images/clipboard-1058954509.png)
You can change the language of your code chunk by changing it between the {}
![](images/clipboard-140854627.png)
If you want to suppress the messages and warnings, or to hide the code, you can set it on your first code chunk with
![](images/clipboard-2187866550.png){width="301"}
or chunk by chunk.
Above, we use five arguments:
- `include = FALSE` prevents code and results from appearing in the finished file.
R Markdown still runs the code in the chunk, and the results can be used by other chunks.
- `echo = FALSE` prevents code, but not the results from appearing in the finished file.
This is a useful way to embed figures.
- `message = FALSE` prevents messages that are generated by code from appearing in the finished file.
- `warning = FALSE` prevents warnings that are generated by code from appearing in the finished.
- `fig.cap = "..."` adds a caption to graphical results.
See the [R Markdown Reference Guide](https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf) for a complete list of knitr chunk options.
## YAML Header {#yaml}
Contains metadata such as the title, author, output format, and date.
All your document settings, such as page size, table of contents, colors, and so on, live here.
Example of a simple YAML header:
``` yaml
---
title: "My Report"
author: "Your Name"
date: "2024-12-10"
output: html_document
number_sections: true
---
```
## Using bookdown
If you want to have more control of the contents of your pdf, for instance the heading numbering and figure numbering related to the heading (Figure 1.2), you may want to use **bookdown** instead of the simple `pdf_output`.
``` yaml
---
output:
bookdown::pdf_document2:
toc: yes
fig_caption: true
---
```
::: {.callout-important appearance="simple"}
Be aware that the yaml header is [very very]{.underline} sensitive to **spaces** and **indentations.**
:::