forked from nhs-r-community/intro-quarto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession-parameters.qmd
138 lines (97 loc) · 2.98 KB
/
session-parameters.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
---
title: "Parameters"
subtitle: "Session - running reports from different inputs"
execute:
echo: fenced
---
```{r}
#| label: packages
#| eval: true
#| echo: false
#| include: false
library(quarto)
```
## Using {quarto}
Writing out the code:
```{r}
quarto::quarto_render("my_report.qmd",
execute_params = list(country = "United_Kingdom"),
output_format = "html"
)
```
. . .
On the Terminal:
```markdown
quarto render my_report.qmd -P country:"United_Kingdom" --to html
```
## More than one parameter
```{r}
quarto::quarto_render("my_report.qmd",
execute_params = list(country = "United_Kingdom",
month_start = "2020-01-01",
month_end = "2020-01-01"),
output_format = "html"
)
```
. . .
On the Terminal:
```markdown
quarto render my_report.qmd -P country:"United_Kingdom" -P month_start:"2020-01-01" -P month_end:"2020-01-01" --to html
```
## Rendering multiple reports
The parameters have been for just one report but if we need to run several in one go we need to a function and a loop in R.
::: {.incremental}
- A function - is a block of code which only runs when its called (Source: [W3Schools](https://www.w3schools.com/r/r_functions.asp))
- A loop - is used for repeating code (and can be used with functions)
:::
## A function
Starts with a name: `run_docs`
```{r}
run_docs <- function() {
}
```
. . .
Takes inputs (called parameters/arguments)
```{r}
run_docs <- function(country, month_start) {
}
```
. . .
Call the {quarto} package which we've used before
```{r}
run_docs <- function(country, month_start) {
quarto::quarto_render(
"my_report.qmd",
output_format = "html",
execute_params = list(country = country,
month_start = month_start)
)
}
```
## Naming the files
In this function we can also use the parameters to create distinct file names so files are created like `Ireland 2020-01-01.html`
```{r}
#| code-line-numbers: 9
run_docs <- function(country, month_begin) {
quarto::quarto_render(
"my_report.qmd",
output_format = "html",
execute_params = list(country = country,
month_start = month_begin),
output_file = glue::glue("{country} {month_begin}.html") # creates the file name
)
}
```
## Using the function
A function can be used individually
```{r}
run_docs("New_Zealand", "2020-01-01")
```
. . .
And for many (in a loop)
```{r}
purrr::map2(c("New_Zealand", "United_Kingdom"), "2020-01-01", run_docs)
```
## Next Section {.smaller}
Credit: Blog by Meghan Hall, [Tips for Custom Parameterized PDFs in Quarto](https://meghan.rbind.io/blog/quarto-pdfs/#parameterized-reports-in-r-and-quarto)
Read more in the blog from Mike Mahoney, [How to use Quarto for Parametized Reporting](https://www.mm218.dev/posts/2022-08-04-how-to-use-quarto-for-parameterized-reporting/#parameterized-reports) including Parametized SQL