quarto presentation code line numbers

Hi! I am trying to following the quarto presentation tutorial for highlighting lines of code: Quarto – Revealjs

I can get the python syntax to render with code highlighting, but I am unable to do the same for R syntax. Is this possible to execute with R as well?

Below is a screenshot example .qmd, which yields the error shown.

The raw .qmd is below, but I couldn't get it to format correctly. Please let me know if you also have tips for formatting chunks/ minimal .qmd's in this forum for readability.

Thank you!

---
title: "Untitled"
format: revealjs
---

## Code 1


```{r, , echo=TRUE, eval=FALSE}
1 + 1
```


## Code 2

```{r, echo=TRUE, eval=FALSE, code-line-numbers = "1"}
1
1:2
```

You encounter an issue because you are using a Quarto format specific option code-line-numbers in usual knitr chunk option syntax. Quarto chunk options have dash in their name and R does not support dash in variable name unless you add specific quote. So this would work

## Code 2

```{r, echo=TRUE, eval=FALSE, `code-line-numbers` = "1"}
1
1:2
```

However, with Quarto, it is better to use the syntax using special comment #|, as it will be more portable between computation engine, and it is easier with Quarto specific options.

## Code 2

```{r, echo=TRUE, eval=FALSE}
#| code-line-numbers: 1
1
1:2
```

you can also set echo and eval in the YAML part of the chunk

Hope it helps

2 Likes

Thank you very much! I could tell something was off, but I could not find it in the documentation. Is it somewhere there that I missed? :face_with_monocle: Or would it be worth submitting a documentation issue in the presentation guide line highlighting section, as all of the chunks demo python code?

The example in this case are all non-compute chunk. They are verbatim code chunks with a class set on them for highlighting language. If they were given for R it would be with your example

```{.r code-line-numbers="1"}
1
1:2
```

You would not use YAML chunk option in this case, as this is not a chunk to execute.

Doc about chunks options is here https://quarto.org/docs/computations/r.html#chunk-options
where the option can be found in the references https://quarto.org/docs/reference/cells/cells-knitr.html

Maybe there could be an example with a executable code chunk to show how to put the option in YAML

1 Like

Thank you for the further explanation!

@cderv Thank you so much for all of these helpful explanations. I have one more follow up question.

I am having trouble finding documentation on the non-compute code chunks indicated by .r. Is this a quarto thing? An rmarkdown thing? Can you point me to any links?

Many thanks, again.

It is usual markdown syntax for code chunk to get them highlighted.
Original doc is in Pandoc as in Rmd and Qmd you're writing Pandoc Markdown. It is called fenced_code_attribute https://pandoc.org/MANUAL.html#extension-fenced_code_attributes

Maybe we should add a paragraph in Quarto – Markdown Basics

Only mention I found in Quarto doc is for visual editor Quarto – Technical Writing

R Markdown and Quarto will output that for you when you use eval = FALSE as only the source code will be included. However, you need computation engine in this case, whereas with the fenced class you don't need any engine.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.