yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Symbols in YAML options (particularly caption) causes errors. #2267

Closed ralmond closed 10 months ago

ralmond commented 1 year ago

Including the characters =, ,, and probably others inside of a caption causes difficulties, and knitr doesn't ignore any special meanings of quoted characters.

Minimal example:

```{r}
#| label: tbl-tab1
#| tbl-cap: "A 3-way array"
#| tbl-subcap:
#|  - "Var3 = 1"
#|  - "Var3 = 2"
arr <- array(1:24,c(4,3,2))
arr[,,1]
arr[,,2]
This produces the error message:

Warning messages: 1: In value[3L] : Failed to parse chunk options in body: Error in parse(text = code, keep.source = FALSE): :1:24: unexpected symbol 1: alist( label: tbl-tab1 tbl ^

2: In opts$engine <- unlist(strsplit(rmdChunkOpts, split = "(\s|,)+"))[[1]] : Coercing LHS to a list

And when run under Quarto does not properly format the chunk (or register the label as a cross-reference).

Here is my session info:

xfun::session_info("knitr") R version 4.3.1 (2023-06-16) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Pop!_OS 22.04 LTS, RStudio 2023.6.0.421

Locale: LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

time zone: America/New_York tzcode source: system (glibc)

Package version: evaluate_0.21 graphics_4.3.1 grDevices_4.3.1 highr_0.10 knitr_1.43 methods_4.3.1 stats_4.3.1
tools_4.3.1 utils_4.3.1 xfun_0.39 yaml_2.3.7



---

By filing an issue to this repo, I promise that

- [ X] I have fully read the issue guide at https://yihui.org/issue/.
- [ X] I have provided the necessary information about my issue.
    - If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    - If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('knitr')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('yihui/knitr')`.
    - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [ X] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.
cderv commented 1 year ago

For the context, this is following report at https://github.com/quarto-dev/quarto/issues/211

As commented there, the warnings are thrown when running chunks interactively in RStudio IDE.

One of the warning is from RStudio code path for sure: https://github.com/rstudio/rstudio/blob/a9a0652e3feac02a05caeead5d54e370f3ec4209/src/cpp/session/modules/SessionRmdNotebook.R#L686

So I believe this is an RStudio IDE issue.

About what is happening, this is related to

#| tbl-subcap:
#|  - "Var3 = 1"
#|  - "Var3 = 2"

If the = are removed, there is no more warning.

And when run under Quarto does not properly format the chunk (or register the label as a cross-reference).

I answer on quarto side. Document is running fine, but crossref feature does not work because the output is not a table. You can't use the table features if the output of the chunk is not a table.

cderv commented 1 year ago

Issue has been opened upstream: https://github.com/rstudio/rstudio/issues/13326

ralmond commented 1 year ago

I left out the calls to kable in the example, as they weren't needed to generate the warning, but they were in the source document. I was still getting the cross-reference problem there, as the label was not getting properly set (see warning) So I think that is probably a secondary effect of the caption parsing problem.

cderv commented 1 year ago

I was still getting the cross-reference problem there, as the label was not getting properly set (see warning) So I think that is probably a secondary effect of the caption parsing problem.

Please to try the example I shared in the Quarto thread

---
title: Table
format: html
---

```{r}
#| label: tbl-tab
#| echo: false
#| tbl-cap: "A 3-way array"
#| tbl-subcap:
#|   - "Var3 = 1"
#|   - "Var3 = 2"
arr <- array(1:24,c(4,3,2))
knitr::kable(arr[,,1])
knitr::kable(arr[,,2])

Look at Table @tbl-tab, just look at it.



This renders correctly with Quarto

![image](https://github.com/yihui/knitr/assets/6791940/3b113698-6fc5-4687-a966-ba0fb66c3b58)

No warning, and no complain about not being able to find cross-reference `@tbl-tab`.

So please, do check you Quarto version, and check that the label use in the chunk is the same as the one in the reference, and also that you code chunk correctly outputs table. 
github-actions[bot] commented 4 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.