Parameters
Session - running reports from different inputs
Using {quarto}
Writing out the code:
```{r}
quarto::quarto_render("covid-analysis.qmd",
execute_params = list(country = "United_Kingdom"),
output_format = "html"
)
```
On the Terminal:
quarto render covid-analysis.qmd -P country:"United_Kingdom" --to html
More than one parameter
```{r}
quarto::quarto_render("covid-analysis.qmd",
execute_params = list(country = "United_Kingdom",
month_start = "2020-01-01",
month_end = "2020-01-01"),
output_format = "html"
)
```
On the Terminal:
quarto render covid-analysis.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 use a function and a loop in R.
- A function - is a block of code which only runs when its called (Source: W3Schools)
- A loop - is used for repeating code (and can be used with functions)
Looping like a roundabout
![Three green fuzzy monsters being whipped around on a spinning park platform, with one that’s already been flung on the ground looking dizzy.]()
Artwork by @allison_horst
A function
Starts with a name: run_docs
```{r}
run_docs <- function() {
}
```
Takes inputs (called parameters/arguments)
```{r}
run_docs <- function(place, time_start) {
}
```
Add in the Quarto code
Call the {quarto} package which we’ve used before
```{r}
run_docs <- function(place, time_start) {
quarto::quarto_render(
"covid-analysis.qmd",
output_format = "html",
execute_params = list(country = place,
month_start = time_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(place, time_start) {
quarto::quarto_render(
"covid-analysis.qmd",
output_format = "html",
execute_params = list(country = place,
month_start = time_start),
output_file = glue::glue("{place} {time_start}.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)
```