Skip to content

Commit 5d16570

Browse files
committed
Add a part in vignette about passing parameters as meta
1 parent 145f62a commit 5d16570

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

vignettes/dynamic-metadata.qmd

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,94 @@ Generated at: {{< meta generated_at >}}
9494
This content would be hidden when debug mode is enabled.
9595
:::
9696

97+
## Advanced Use Case: Conditional Content Based on parameters
98+
99+
Another powerful use case is making Quarto parameters available as metadata for conditional content. This allows you to control document behavior through parameters while leveraging Quarto's conditional content features.
100+
101+
Here's an example that demonstrates creating different versions of a sales report based on parameters:
102+
103+
````markdown
104+
---
105+
title: "Sales Report"
106+
format: html
107+
params:
108+
region: "North America"
109+
show_confidential: false
110+
quarter: "Q1"
111+
---
112+
113+
```{{r}}
114+
#| echo: false
115+
#| output: asis
116+
quarto::write_yaml_metadata_block(
117+
params = params
118+
)
119+
```
120+
121+
# {{< meta params.quarter >}} Sales Report - {{< meta params.region >}}
122+
123+
::: {.content-visible when-meta="params.show_confidential"}
124+
::: {.callout-warning}
125+
## Confidential Information
126+
This section contains sensitive financial data and competitor analysis.
127+
128+
Region: {{< meta params.region >}}
129+
Quarter: {{< meta params.quarter >}}
130+
:::
131+
132+
```{{r}}
133+
# Show detailed financial breakdown
134+
cat("Detailed revenue breakdown by product line...")
135+
cat("\nConfidential metrics and competitor analysis...")
136+
```
137+
:::
138+
139+
::: {.content-visible unless-meta="params.show_confidential"}
140+
::: {.callout-note}
141+
## Public Summary
142+
This report shows general performance metrics suitable for public distribution.
143+
144+
Region: `{{r}} params$region`
145+
Quarter: `{{r}} params$quarter`
146+
:::
147+
148+
```{{r}}
149+
# Show summary metrics only
150+
cat("Overall performance summary for", params$region)
151+
cat("\nPublic-facing metrics for", params$quarter)
152+
```
153+
:::
154+
````
155+
156+
This approach is particularly useful for:
157+
158+
1. **Parameterized reporting**: Generate different document versions based on input parameters
159+
2. **Conditional content**: Show or hide sections dynamically based on computed values
160+
3. **Document customization**: Tailor content and presentation for different contexts or audiences
161+
4. **Workflow automation**: Control document behavior programmatically through parameter passing
162+
163+
You can render different versions by passing parameters:
164+
165+
```r
166+
# Internal report with confidential data
167+
quarto::quarto_render("sales-report.qmd",
168+
execute_params = list(
169+
region = "North America",
170+
show_confidential = TRUE,
171+
quarter = "Q2"
172+
))
173+
174+
# Public report without confidential data
175+
quarto::quarto_render("sales-report.qmd",
176+
execute_params = list(
177+
region = "Europe",
178+
show_confidential = FALSE,
179+
quarter = "Q2"
180+
))
181+
```
182+
183+
The key insight is that `write_yaml_metadata_block(params = params)` makes all your document parameters available as metadata. The boolean ones can then be used with Quarto's `when-meta` and `unless-meta` conditional attributes for dynamic content control.
184+
97185
## Advanced Use Case: Email Variant Testing
98186

99187
One powerful application of dynamic metadata is variant emails using Quarto's email format. This example shows how to randomly select an email variant and conditionally display different content based on that selection:

0 commit comments

Comments
 (0)