quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.85k stars 313 forks source link

Partial matching of parameters is confusing #7393

Open mine-cetinkaya-rundel opened 11 months ago

mine-cetinkaya-rundel commented 11 months ago

In Quarto 1.4.451, create the following dashboard with parameters:

---
title: "Sample dashboard"
format: dashboard
engine: knitr
params:
  hello: 1
  mello: "a character"
---

This should print out the value of `params$hello`: `{r} params$hello`.

This should print out the value of `params$mello`: `{r} params$mello`.

This shouldn't work because it's looking for a parameter that doesn't exist, `params$jello`: `{r} params$jello`.

This also shouldn't work because it's looking for a parameter that doesn't exist, `params$m`, but it does partial matching to `params$mello`, and the document renders: `{r} params$m`

It works, but I think it would be better if partial matching wasn't enabled by default for parameters.

Screenshot 2023-10-29 at 2 44 29 AM

Then add the following and re-render:

This doesn't work because it's doing partial matching and then failing when adding a number to a character `params$m + 2`: `{r} params$m + 2`.

You get the following error:

Quitting from lines 2-18 (untitled.qmd)
Error in `params$m + 2`:
! non-numeric argument to binary operator
Backtrace:
 1. .QuartoInlineRender(params$m + 2)

Execution halted

It makes sense why this happens, but again, not doing partial matching by default would be less confusing.

Additionally, if I set partial matching to warn in the YAML of my document, e.g.,

knitr:
    opts_chunk:
      R.options:
        warnPartialMatchDollar: true

the first example with partial matching still runs, which was even more surprising to me.

mcanouil commented 11 months ago

I am personally not sure this should be changed as it is R/knitr specific and the default R behaviour. Was this different in Rmarkdown? (I never use partial matching in R)

cderv commented 11 months ago

It happens also with rmarkdown

---
title: "Sample dashboard"
output: html_document
params:
  hello: 1
  mello: "a character"
---

This should print out the value of `params$hello`: `r params$hello`.

This should print out the value of `params$mello`: `r params$mello`.

This shouldn't work because it's looking for a parameter that doesn't exist, `params$jello`: `r params$jello`.

This also shouldn't work because it's looking for a parameter that doesn't exist, `params$m`, but it does partial matching to `params$mello`, and the document renders: `r params$m`

The R options does not work when pass to opts_chunk because it is too late. params are not evaluated as part of a chunk.

However, if you do set the options before rendering you'll see the warning

> withr::with_options(list(warnPartialMatchDollar = TRUE), rmarkdown::render("test.Rmd"))

processing file: test.Rmd

output file: test.knit.md

"C:/Users/chris/scoop/shims/pandoc" +RTS -K512m -RTS test.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.html --lua-filter "C:\Users\chris\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\chris\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --variable bs3=TRUE --section-divs --template "C:\Users\chris\AppData\Local\R\win-library\4.3\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable "mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-in-header "C:\Users\chris\AppData\Local\Temp\Rtmp0MhBRB\rmarkdown-str9a3c29ea589e.html" 

Output created: test.html
Message d'avis :
Dans params$m : correspondance partielle de 'm' en 'mello'

Partial Matching is really a R thing that happens with $ operator. It is on by default, and I don't think it can be deactivated. Good practice to avoid it is

I don't think there is anything we can do here as this is related to how R works, and not really Quarto, dashboard, knitr, or rmarkdown.

Though we could think of something - like decide to activate the warning options for a quarto rendering maybe...