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

Evaluating R code with !expr in fig-subcap doesn't work #2047

Closed
7 tasks done
svraka opened this issue Aug 22, 2022 · 9 comments
Closed
7 tasks done

Evaluating R code with !expr in fig-subcap doesn't work #2047

svraka opened this issue Aug 22, 2022 · 9 comments
Labels
bug Something isn't working yaml-validation Issues with YAML validation and autocompletion in quarto
Milestone

Comments

@svraka
Copy link

svraka commented Aug 22, 2022

Bug description

It is documented that !expr works with fig-cap, but it doesn't work with fig-subcap.

```{r}
#| label: fig-charts
#| fig-cap: Charts
#| fig-subcap: !expr c("First", "Second")

plot(cars)
plot(pressure)
```

I get the following error when running quarto render on the file:

ERROR: Validation of YAML cell metadata failed.
In file subfig-eval-yaml.qmd
(line 4, columns 16--42) Key fig-subcap has value !expr c("First", "Second"), which must be 'true'
3: #| fig-cap: Charts
4: #| fig-subcap: !expr c("First", "Second")
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~

ERROR: Render failed due to invalid YAML.

quarto check Output

[>] Checking Quarto installation......OK
      Version: 1.0.38
      Path: C:\scoop\apps\quarto\current\bin\
      CodePage: 1250

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

[>] Checking Python 3 installation....OK
      Version: 3.10.6
      Path: C:/msys64/mingw64/bin/python.exe
      Jupyter: (None)

      Jupyter is not available in this Python installation.
      Install with python -m pip install jupyter

[>] Checking R installation...........OK
      Version: 4.2.1
      Path: C:/scoop/apps/r-release/current
      LibPaths:
        - C:/scoop/apps/r-release/4.2.1/library
      rmarkdown: 2.15

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

quarto tools check Output

[>] Inspecting tools

Tool         Status                    Installed     Latest  
chromium     Not installed             ---           869685  
tinytex      External Installation     ---           v2022.08

Checklist

  • formatted your issue so it is easier for us to read?
  • included a minimal, fully reproducible example in a single .qmd file? Please provide the whole file rather than the snippet you believe is causing the issue.
  • documented the quarto version you're running, by pasting the output from running quarto check in the "Quarto Check Output" text area?
  • documented the version of the quarto tools you're running, by providing the output from running quarto tools check in the "Quarto Tools Check Output" text area?
  • documented the RStudio IDE version you're running (if applicable), by providing the value displayed in the "About RStudio" main menu dialog?
  • documented which operating system you're running? If on Linux, please provide the specific distribution as well.
  • upgraded to the latest version, including your versions of R, the RStudio IDE, and relevant R packages?
@svraka svraka added the bug Something isn't working label Aug 22, 2022
@cscheid cscheid removed the bug Something isn't working label Aug 22, 2022
@cscheid
Copy link
Collaborator

cscheid commented Aug 22, 2022

Can you upgrade to the latest prerelease version of quarto here? https://github.com/quarto-dev/quarto-cli/releases/tag/v1.1.109

You're likely seeing what's not a bug, but rather an issue in your YAML: !expr needs a string parameter, and so you need to escape those quotes. The latest version has better error messages in this case.

@cscheid cscheid added the needs-repro Issues that are blocked until reporter provides an adequate reproduction label Aug 22, 2022
@svraka
Copy link
Author

svraka commented Aug 22, 2022

Can you upgrade to the latest prerelease version of quarto here? https://github.com/quarto-dev/quarto-cli/releases/tag/v1.1.109

Error is the same with 1.1.109.

You're likely seeing what's not a bug, but rather an issue in your YAML: !expr needs a string parameter, and so you need to escape those quotes. The latest version has better error messages in this case.

I dont't understand your comment. The same code with fig-cap instead of fig-subcap works and doesn't need any escaping---as it is explained in the documentation I linked.

```{r}
#| label: fig-charts
#| fig-cap: !expr c("First", "Second")

plot(cars)
plot(pressure)
```

@cscheid cscheid added bug Something isn't working and removed needs-repro Issues that are blocked until reporter provides an adequate reproduction labels Aug 22, 2022
@cscheid cscheid added this to the Future milestone Aug 22, 2022
@cscheid cscheid added the yaml-validation Issues with YAML validation and autocompletion in quarto label Aug 22, 2022
@cscheid
Copy link
Collaborator

cscheid commented Aug 22, 2022

I apologize - I read the error message entirely wrong.

Unfortunately, this won't be easy for us to fix right now; The issue is that the validator sees a datatype of string, and can't in general know that the output you're generating is an array of strings. You can work around this issue by disabling yaml validation for the entire document, with

---
validate-yaml: false
---

But this means there won't be any validation of yaml across the entire quarto document.

@svraka
Copy link
Author

svraka commented Aug 23, 2022

validate-yaml: false works indeed.

Does this mean you have special code to handle fig-cap? Or how else can fig-cap: !expr c("First", "Second") be valid YAML, while fig-subcap: !expr c("First", "Second") is not?

@cscheid
Copy link
Collaborator

cscheid commented Aug 23, 2022

It's not that we have special r code for it. It's that the type of fig-subcap is "array of strings", while the type of !expr c("1", "2") (while in YAML) is "string with tag expr".

@schwa021
Copy link

schwa021 commented Apr 5, 2023

validate-yaml: false works indeed.

Does this mean you have special code to handle fig-cap? Or how else can fig-cap: !expr c("First", "Second") be valid YAML, while fig-subcap: !expr c("First", "Second") is not?

FYI - I just tried this 1.3.306 and it worked fine without having to invalidate the YAML. Perhaps this has been fixed?

@egouldo
Copy link

egouldo commented Sep 11, 2023

validate-yaml: false works indeed.
Does this mean you have special code to handle fig-cap? Or how else can fig-cap: !expr c("First", "Second") be valid YAML, while fig-subcap: !expr c("First", "Second") is not?

FYI - I just tried this 1.3.306 and it worked fine without having to invalidate the YAML. Perhaps this has been fixed?

Just downloaded and installed 1.3.450 and it's not working for me.

@cderv
Copy link
Collaborator

cderv commented Sep 28, 2023

So I believe this works ok now, at least using Quarto 1.4 which is pre-release.

---
title: "test"
format: html
---

```{r}
#| label: fig-charts
#| fig-cap: Charts
#| fig-subcap: !expr c("First", "Second")

plot(cars)
plot(pressure)
```

image

You'll still get the warning in YAML linting for what is mentioned already in this thread.

So you should not need R to create a list

```{r}
#| label: fig-charts
#| fig-cap: Charts
#| fig-subcap: 
#|   - First
#|   - Second

plot(cars)
plot(pressure)
```

New cross ref system allows you to express with Div figures and could be interesting to use inline code inside captions for example to avoid use of !expr when you need add a caption with text from evaluation

---
title: "test"
format: html
---

See @fig-example-multiple-chunks, @fig-subfig-1 and @fig-subfig-2.

::: {#fig-example-multiple-chunks layout="[[ 25, 75 ]]"}

::: {#fig-subfig-1}
```{r}
#| echo: false
#| fig-width: 2
#| fig-height: 5
plot(cars)
```

This dataset has `r nrow(cars)` rows.

:::

```{r}
#| label: fig-subfig-2
#| fig-cap: "I don't care about the number of rows on this one."
#| echo: false
#| fig-width: 6
#| fig-height: 5
plot(pressure)
```

Charts

:::

Are we keeping this open to deal with YAML linting improvment about !expr ?
If so we could maybe rename the issue to make that clear 🤷

@cderv
Copy link
Collaborator

cderv commented Feb 10, 2025

Let's close this issue now that we have one report specific to !expr

@cderv cderv closed this as completed Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working yaml-validation Issues with YAML validation and autocompletion in quarto
Projects
None yet
Development

No branches or pull requests

5 participants