yihui / knitr-examples

A collection of knitr examples
483 stars 588 forks source link

knitr syntax #69

Open rundel opened 3 years ago

rundel commented 3 years ago

I've been working on a parser for RMarkdown documents using Boost's Spirit X3 library. I'm currently using the Rmds available within this repo as a test set to make sure my code is working correctly.

In running my parser I've come across some potential typos and inconsistencies in syntax in the example documents and I wanted to clarify.

Mismatched backticks

Chunk begins with 3 backticks and ends with 4, it seems like knitr is not checking for balance between the backticks as long as there is >= 3. Oddly, for the document below RStudio gives me an unexpected token error via the linter but I don't see a similar error when creating my own test documents.

Chunks missing closing backticks

There are a couple of examples where there are two sequential chunks where the first is missing its closing backticks, but it is immediately followed by the start of a new chunk. The documents compile correctly but this seems wrong to me.

Chunks with engine and name separated by a comma

There are several documents where the chunk has a format that looks like

```{engine, name, opt=val}

my understanding is that the prefered format should be

```{engine name, opt=val}

is the former syntax allowable?

yihui commented 3 years ago

Chunk begins with 3 backticks and ends with 4, it seems like knitr is not checking for balance between the backticks as long as there is >= 3.

That's true. I just fixed the example anyway.

There are a couple of examples where there are two sequential chunks where the first is missing its closing backticks, but it is immediately followed by the start of a new chunk.

Closing backticks are not required before a new code chunk is opened. That's from Knuth's literate programming. Section 5.1.4 has more info about it: https://www.google.com/books/edition/Dynamic_Documents_with_R_and_knitr/fyIuDwAAQBAJ?hl=en&gbpv=1&bsq=%20literate%20programming

Chunks with engine and name separated by a comma

Either a comma or a space can serve as the separator.

Thank you!

rundel commented 3 years ago

Closing backticks are not required before a new code chunk is opened. That's from Knuth's literate programming. Section 5.1.4 has more info about it: https://www.google.com/books/edition/Dynamic_Documents_with_R_and_knitr/fyIuDwAAQBAJ?hl=en&gbpv=1&bsq=%20literate%20programming

Interesting, I wasn't familiar with this.