-
DescriptionI want to do parametrised analyses modules, which would be conditionally evaluated affecting both titles and chunks, so that the titles aren't printed on the output but are still on the .qmd file for navigation purposes. I have already tried the combination of The following option also is not what I want because it'll make the title not available for navigation:
Now I am trying to create my own shortcode or lua filter that achieves this purpose. This is currently my first attempt, any help it'll be welcome:
function Meta(meta)
modules = meta.params.module or {} -- Access the 'module' field in YAML
end
function Div(el)
local module_name = el.attributes['module']
if module_name and modules[module_name] == false then
return {} -- Hide content
end
return el -- Otherwise, render content as usual
end
---
title: "analysis"
format:
html:
echo: false
warning: false
message: false
params:
module:
pca: false
editor: visual
---
{{< include module-dimred.qmd >}}
---
title: ""
format: html
filters:
- conditional_evaluation.lua
---
::: {module="pca"}
### Principal Component Analysis (PCA)
```{r}
#| eval: !expr params$module$pca
library(ggpubr)
set.seed(123)
pca_results <- prcomp(iris[,1:4], scale. = TRUE)
pca_df <- data.frame(Species=iris$Species, PC1 = pca_results$x[,1], PC2 = pca_results$x[,2])
ggscatter(pca_df, x="PC1",y="PC2", color="Species")
```
::: |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
A Lua filter could avoid the repetition but I just though of the following workaround. ---
title: "Quarto Playground"
format: html
params:
pca: true
execute:
echo: false
---
::: {.content-hidden}
### Principal Component Analysis (PCA)
:::
```{r}
#| eval: !expr isTRUE(params[["pca"]])
#| output: asis
cat("\n\n#### Principal Component Analysis (PCA)\n\n")
```
```{r}
#| eval: !expr isTRUE(params[["pca"]])
plot(1)
``` |
Beta Was this translation helpful? Give feedback.
-
This works with the behaviour I intended, but I cannot express enough how practical would be that there's a conditional evaluation at the level of the shortcode, not only for avoiding redundancy (expliciting twice the eval and hiding the title), but because it would be compatible with other shortcodes and for example, I could do a .{panel-tabset} that shows only the panels that were evaluated. I think one of the strengths of Quarto is the use of shortcodes, which are compatible with the visual editor too, and the amazing things that could be done with a conditional-evaluation, not only a .content-hidden, would be awesome. I'll try, when I have more time, to implement my own lua filter. |
Beta Was this translation helpful? Give feedback.
-
I second @joanbadia's request. If the existing shortcode would be enhanced to support |
Beta Was this translation helpful? Give feedback.
-
Thanks @mcanouil for the clarification and sorry for using the wrong terminology. I know that I'd like to declare a metadata entry, e.g. "customer". In a document with say 95% of content valid for all customers, 5% is customer specific. Instead of defining a metadata entry for each customer, I'd like to assign the customer value when rendering the document (e. g. with Hence my suggestion to enhance the existing conditional This use case might be slightly different from that of the OP but I think it also might serve him. And I'm aware that only parameters can be used for R code chunks. |
Beta Was this translation helpful? Give feedback.
-
I think it's time to close this topic because I no longer believe a lua filter can affect both code and fenced divs / shortcodes. Lua filters are applied after the code chunks are evaluated, so it wouldn't prevent a code chunk from being evaluated and at much it could hide its content (which is not what I want). Nonetheless I had good progress in other points:
I'll copy my final workflow, in case anyone is interested:
|
Beta Was this translation helpful? Give feedback.
I think it's time to close this topic because I no longer believe a lua filter can affect both code and fenced divs / shortcodes. Lua filters are applied after the code chunks are evaluated, so it wouldn't prevent a code chunk from being evaluated and at much it could hide its content (which is not what I want).
Nonetheless I had good progress in other points:
pca: …