Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean parameter in quarto_render is not interpreted by .content-visible whith when-meta option #211

Closed
papayoun opened this issue Sep 10, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@papayoun
Copy link

Bug description

I have a parametrized quarto document with a boolean parameter named correction.
This boolean controls both R code execution (using params$correction) and text printing via the when-meta option for a class .content-visible (see the minimal example below).

When I knit the document using the Render button on Rstudio, eveything works fine, both the code execution and text printing reacts according to the value of correction attribute.

However, when I use quarto_render, and then give the value of correction as a R boolean, the .content-visible part do not reacts accordingly to the given value

Steps to reproduce

Create a my_document.qmd file containing the following

---
format: 
  pdf: default
params:
  correction: false
---

```{r}
print(class(params$correction))
```

```{r}
#| label: chunk-R-condional_code
#| eval: !expr "params$correction"
print("This chunk is executed only on correction")
```

:::{.content-visible when-meta="params.correction"}

This text should appear when correction is set to `TRUE` 

:::

You can ensure that everythings works fine when you render it from Rstudio by clicking on the Render button.

Then, in R, execute:

sapply(c(FALSE, TRUE), function(cor_){
  output_name <- ifelse(cor_, "cor_TRUE.pdf", "cor_FALSE.pdf") 
  quarto::quarto_render(input = "my_document.qmd",  
                        execute_params = list(correction  = cor_), 
                        output_format = "pdf",
                        output_file = output_name)
})

Expected behavior

The R code should produce a document cor_TRUE.pdf where both the print from the R code and the text within the .content-visible appears and a document cor_FALSE.pdf where none of those appears.

Actual behavior

The R code interprets well the given parameter, but it is ignored by the .content-visible class.

Your environment

  • IDE: Rstudio "Chocolate Cosmos" Release (e4392fc9, 2024-06-05) for Ubuntu Jammy
  • R: R version 4.4.1 (2024-06-14) -- "Race for Your Life"
  • Quarto: 1.5.57
  • R quarto package: 1.4.4

Quarto check output

Quarto 1.5.57
[✓] Checking versions of quarto binary dependencies...
Pandoc version 3.2.0: OK
Dart Sass version 1.70.0: OK
Deno version 1.41.0: OK
Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
Version: 1.5.57
Path: /opt/quarto/bin

[✓] Checking tools....................OK
TinyTeX: v2024.09
Chromium: (not installed)

[✓] Checking LaTeX....................OK
Using: TinyTex
Path: /home/gloaguen/.TinyTeX/bin/x86_64-linux
Version: 2024

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
Version: 3.11.7 (Conda)
Path: /home/gloaguen/miniconda3/envs/STA2121/bin/python
Jupyter: 5.7.1
Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
Version: 4.4.1
Path: /usr/lib/R
LibPaths:
- /home/gloaguen/R/x86_64-pc-linux-gnu-library/4.4
- /usr/local/lib/R/site-library
- /usr/lib/R/site-library
- /usr/lib/R/library
knitr: 1.48
rmarkdown: 2.28

[✓] Checking Knitr engine render......OK

@papayoun papayoun added the bug Something isn't working label Sep 10, 2024
@papayoun papayoun changed the title Boolean parameter in quarto_render is not interpreted by then when-meta option Boolean parameter in quarto_render is not interpreted by .content-visible whith when-meta option Sep 10, 2024
@cderv cderv transferred this issue from quarto-dev/quarto-cli Sep 10, 2024
@cderv
Copy link
Collaborator

cderv commented Sep 10, 2024

This is from the doc about when-meta (https://quarto.org/docs/authoring/conditional.html#matching-against-metadata)

It’s possible to match against boolean metadata values. Use the attributes unless-meta and when-meta, and use periods . to separate metadata keys

Now let's look at your example

params:
  correction: false

In your document, you correctly define correction as a boolean. So when you render this document using Render button, the params$correction get set based on default value. Also the metadata is retrieved from the YAML header and correctly read through when-meta

However, this Quarto feature when-meta is different than a parameter from knitr. This means that when you set params field it won't set change the metadata in the YAML header.

So params meta is always defined to what you set in the YAML header.

Currently, you need to explicitly redefine the meta - soon with a helper function (#137)

Example document
---
format: 
  pdf: default
params:
  correction: true
---

```{r}
print(class(params$correction))
```

```{r}
#| label: chunk-R-condional_code
#| eval: !expr "params$correction"
print("This chunk is executed only on correction")
```

```{r}
#| include: false
write_meta <- function(meta) {
  handlers <- list(logical = function(x) {
        value <- ifelse(x, "true", "false")
        structure(value, class = "verbatim")
    })
  res <- yaml::as.yaml(meta, handlers = handlers)
  knitr::asis_output(paste0("---\n", res, "---\n"))
}
```

```{r}
#| echo: false
#| output: asis
write_meta(list(params = list(correction = params$correction)))
```

:::{.content-visible when-meta="params.correction"}

This text should appear when correction is set to `TRUE` 

:::

I moved this to this repo as I though it was a R package issue, but it may something we need to improve.

quarto render index.qmd  -P correction:false

This won't modify the params inside the document YAML header for knitr engine.

Let's note that it requires a new parameter interface multi engine before we can improve this. So related to

@papayoun
Copy link
Author

papayoun commented Sep 10, 2024

Thank very much for the quick answer and for the working solution!
I agree with you that the current solution might be improved, but nevertheless, thanks for the already amazing job on quarto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants