From 0b688ed724fbd34fa0db8d516805264143ba6e24 Mon Sep 17 00:00:00 2001 From: Joao-O-Santos Date: Tue, 29 Jul 2025 02:36:15 +0100 Subject: [PATCH 1/4] Add explanation on conditional code execution --- docs/authoring/conditional.qmd | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/authoring/conditional.qmd b/docs/authoring/conditional.qmd index 33b11cafb4..9d1c36c6fb 100644 --- a/docs/authoring/conditional.qmd +++ b/docs/authoring/conditional.qmd @@ -25,6 +25,16 @@ You can also set conditions on non-executable code blocks: ``` ```` +When using the `knitr` engine, if you want to have a code chunk being excecuted/evaluated only when the output is being rendered to HTML or LaTeX you can use the `is_html_output()` and `is_latex_output()` respectively (see [this reply to quarto-cli issue 11724](https://github.com/quarto-dev/quarto-cli/issues/11724#issuecomment-2557635192)). + +```` markdown +``` {r .content-visible when-format="html"} +#| eval = !expr knitr::is_html_output() +# code is shown AND evaluated only in HTML +2+2 +``` +```` + To apply a condition only to a part of a paragraph, use a span (`[]{}`): ``` markdown From 876fbe0eece04b8c1ea81e557e2c972efd84d5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20O=2E=20Santos?= Date: Tue, 5 Aug 2025 14:53:49 +0100 Subject: [PATCH 2/4] Fix typo in PR for docs/authoring/conditional.qmd Co-authored-by: Christophe Dervieux --- docs/authoring/conditional.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/authoring/conditional.qmd b/docs/authoring/conditional.qmd index 9d1c36c6fb..8321f1cc4c 100644 --- a/docs/authoring/conditional.qmd +++ b/docs/authoring/conditional.qmd @@ -25,7 +25,7 @@ You can also set conditions on non-executable code blocks: ``` ```` -When using the `knitr` engine, if you want to have a code chunk being excecuted/evaluated only when the output is being rendered to HTML or LaTeX you can use the `is_html_output()` and `is_latex_output()` respectively (see [this reply to quarto-cli issue 11724](https://github.com/quarto-dev/quarto-cli/issues/11724#issuecomment-2557635192)). +When using the `knitr` engine, if you want to have a code chunk being executed/evaluated only when the output is being rendered to HTML or LaTeX you can use the `is_html_output()` and `is_latex_output()` respectively (see [this reply to quarto-cli issue 11724](https://github.com/quarto-dev/quarto-cli/issues/11724#issuecomment-2557635192)). ```` markdown ``` {r .content-visible when-format="html"} From 65e3bb994c7c8438de2774a2fe3c73fd8b23da5b Mon Sep 17 00:00:00 2001 From: Joao-O-Santos Date: Tue, 5 Aug 2025 15:25:47 +0100 Subject: [PATCH 3/4] Fix errors and make it a collapsible tip --- docs/authoring/conditional.qmd | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/authoring/conditional.qmd b/docs/authoring/conditional.qmd index 8321f1cc4c..25fc70f649 100644 --- a/docs/authoring/conditional.qmd +++ b/docs/authoring/conditional.qmd @@ -25,16 +25,23 @@ You can also set conditions on non-executable code blocks: ``` ```` -When using the `knitr` engine, if you want to have a code chunk being executed/evaluated only when the output is being rendered to HTML or LaTeX you can use the `is_html_output()` and `is_latex_output()` respectively (see [this reply to quarto-cli issue 11724](https://github.com/quarto-dev/quarto-cli/issues/11724#issuecomment-2557635192)). +::: {.callout-tip title="Conditional Exectution with Knitr" collapse="true"} + +When using the `knitr` engine, if you want to have an **executable** code chunk being executed/evaluated only when the output is being rendered to HTML or LaTeX you can use the `is_html_output()` and `is_latex_output()` respectively (see [this reply to quarto-cli issue 11724](https://github.com/quarto-dev/quarto-cli/issues/11724#issuecomment-2557635192)). ```` markdown -``` {r .content-visible when-format="html"} -#| eval = !expr knitr::is_html_output() +::: {.content-visible when-format="html"} +``` {r} +#| eval: !expr knitr::is_html_output() # code is shown AND evaluated only in HTML 2+2 ``` + +::: ```` +::: + To apply a condition only to a part of a paragraph, use a span (`[]{}`): ``` markdown @@ -122,4 +129,4 @@ path: ```` This feature is often useful alongside [project profiles](/docs/projects/profiles.qmd). -Different profiles can set different metadata values, and so can control the metadata used in conditional content. \ No newline at end of file +Different profiles can set different metadata values, and so can control the metadata used in conditional content. From 20112aad104a4e6cb30766ad32473f1c8387a051 Mon Sep 17 00:00:00 2001 From: Joao-O-Santos Date: Tue, 5 Aug 2025 15:49:22 +0100 Subject: [PATCH 4/4] Fix bugs that caused improper rendering of chunk --- docs/authoring/conditional.qmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/authoring/conditional.qmd b/docs/authoring/conditional.qmd index 25fc70f649..07c63811f0 100644 --- a/docs/authoring/conditional.qmd +++ b/docs/authoring/conditional.qmd @@ -31,7 +31,8 @@ When using the `knitr` engine, if you want to have an **executable** code chunk ```` markdown ::: {.content-visible when-format="html"} -``` {r} + +```{{r}} #| eval: !expr knitr::is_html_output() # code is shown AND evaluated only in HTML 2+2 @@ -39,7 +40,6 @@ When using the `knitr` engine, if you want to have an **executable** code chunk ::: ```` - ::: To apply a condition only to a part of a paragraph, use a span (`[]{}`):