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

YAML chunk options inconsistent use of : and = #863

Closed
plukethep opened this issue May 13, 2022 · 5 comments
Closed

YAML chunk options inconsistent use of : and = #863

plukethep opened this issue May 13, 2022 · 5 comments

Comments

@plukethep
Copy link

Hi, please see this:

The following YAML bring up an error

```{r}
#| echo: c(3)
month <- "July"

str(month)

But using an = sign works:
#| echo= c(3)
month <- "July"

str(month)

https://stackoverflow.com/questions/72217651/quarto-rmarkdown-code-block-to-only-display-certain-lines
@cderv
Copy link
Collaborator

cderv commented May 13, 2022

This is expected as you are mixing syntax in the example above. Let me explain.

About the different format

Chunk option in Quarto can be set using YAML syntax, prepending |#

```{r}
#| label: demo
#| echo: 3
month <- "July"

str(month)
```

This syntax can be use with any computation engine, include the OJS, Julia and Jupyter supported by Quarto.

When using R, this is equivalent to using option in chunk header using valid R syntax

```{r, label = "demo", echo = c(3))}
month <- "July"

str(month)
```

In fact, knitr supports the syntax natively in R Markdown document too. There is also support for using multiline chunk options using R syntax

```{r}
#| label = "demo", eval = TRUE,
#| echo = c(3)
month <- "July"

str(month)
```

This is specific to knitr computation engine, and can also be used within Quarto.

All this format supported in knitr have been announced in this blog post https://yihui.org/en/2022/01/knitr-news/

Why different format ?

The YAML syntax is mainly here because it allows Quarto to use the same syntax for any computation engine, and not just R.
One major drawback is that you cannot easily execute R code (like using R variable in chunk option) as this is not directly R syntax and will be parsed as YAML, not R. If you need that, you need to use special !expr syntax with R engine : this is explained in https://quarto.org/docs/computations/r.html#chunk-options

The other syntax are native with R - so you can use any valid R code as chunk option. The multiline version is here by convenience so that you can split in several lines long chunk options. Example from blog post linked above:

```{r}
#| rmdworkflow,
#| echo = FALSE,
#| fig.cap = "A diagram illustrating how an R Markdown document
#|   is converted to the final output document.",
#| out.width = "100%"

knitr::include_graphics("images/workflow.png", dpi = NA)
```

The advantage is that you can use R variable without any special character.

It will work in Quarto but this is specific to knitr and won't obviously work with Python or else.

About your example

In your example, you are using #| echo: c(3) : this is not valid YAML. However echo= c(3) is valid R code.

That is why you see an error in the first case - it needs to be valide YAML or using !expr for R parsing before YAML parsing.
So it would be

```{r}
#| label: demo2
#| echo: 3
month <- "July"

str(month)
```

```{r}
#| label: demo4
#| echo: !expr c(3)
month <- "July"

str(month)
```

I hope this clarify how this works.

Full example
---
title: "My simple title"
author: "My name and ID"
format: html
---

# Using header option

```{r, label = "demo", echo = c(3)}
month <- "July"

str(month)
```

# Using multiline syntax

```{r}
#| label = "demo1", eval = TRUE,
#| echo = c(3)
month <- "July"

str(month)
```

# Using YAML syntax

```{r}
#| label: demo2
#| echo: 3
month <- "July"

str(month)
```

# Using yaml with R code to parse

```{r}
#| label: demo4
#| echo: !expr c(3)
month <- "July"

str(month)
```

@plukethep
Copy link
Author

Many thanks both, apologies, I'm new to quarto!

@cderv
Copy link
Collaborator

cderv commented May 13, 2022

No need to be sorry. It means things as not as clear as they should. It can be confusing to support multi syntax but with Quarto we are only advertising one (the YAML syntax) as this is the one that is portable across engine. You should use that one first for new document.

For old Rmd document, no need to convert - the usual syntax is working fine with Quarto too.

@jeweljohnsonj
Copy link

A related question: is it possible to hide the output of certain lines of code? For example;

```{r}
mtcars
rmarkdown::paged_table(mtcars)
```

Can I show 'mtcars' in the code chunk after the render but not 'rmarkdown::paged_table(mtcars)' and in addition to that have only the result of the paged_table() shown?

@cderv
Copy link
Collaborator

cderv commented May 20, 2022

@jeweljohnsonj It is not directly a Quarto question, and not related to this issue. Please next time ask in the right place.
https://community.rstudio.com is probably the best place

Can I show 'mtcars' in the code chunk after the render but not 'rmarkdown::paged_table(mtcars)' and in addition to that have only the result of the paged_table() shown?

Best way would be

```{r, eval= FALSE}
mtcars
```

```{r, echo = FALSE}
rmarkdown::paged_table(mtcars)
```

This will work in Quarto

I would have try this:

```{r, echo = -2, eval= 2}
mtcars
rmarkdown::paged_table(mtcars)
```

but it will add ## before the unevaluated line, which is not ideal but here to show that the line has not been evaluated really.
I'll see if we should allow this.

Hope it helps.

@plukethep I am closing this, I think we're good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants