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

R Markdown in tab indented code block raises duplicated label error #443

Closed
jeromyanglim opened this issue Dec 20, 2012 · 8 comments
Closed
Milestone

Comments

@jeromyanglim
Copy link

I have a tutorial where R Markdown code blocks are displayed as formatted code blocks. I was using knitr 0.8 and the same problem seems to occur after updating to version 0.9.

Here is a minimal example

Here is some formatted code:

    ```{r foo}
    print('hello world!')
    ```
It displays 

```{r foo}
print('hello world!')
```

This yields the following error:

Error in parse_block(g) : duplicated label 'foo'
Calls: knit ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted

knitr terminated with status 1

I assume that the parser is not realising that R markdown in code blocks is not a real R Markdown code block . When I originally wrote the tutorial, I didn't have this problem.

Update: I just noticed when I updated the code blocks to remove the labels, that tab indented code blocks are run as regular R code chunks. Is this just the new intended behaviour of knitr that R code chunks don't have to start and end at the start of a new line?

@ramnathv
Copy link
Contributor

Yes. I believe it was modified in 0.80. @yihui has an example showing how to use hooks to print a code chunk verbatim especially useful in tutorials https://github.com/yihui/knitr-examples/blob/master/062-chunk-wrapper.Rmd
It is not perfect, as it still does not display the entire chunk in one block, but I believe it can be tweaked easily to get the result desired for tutorials.

@ramnathv
Copy link
Contributor

Here is the relevant change from NEWS.md

leading spaces are allowed in chunk headers now, e.g. in the past <<>>= must appear in the beginning of a line, but now we can indent the chunk header by a number of white spaces; this amount of spaces will be stripped off the chunk if the whole chunk is indented (#236) (thanks, @jamiefolson and Vitalie Spinu)

@jeromyanglim
Copy link
Author

Thanks for that. The behaviour certainly surprised me, but I suppose it only really affects people writing R Markdown tutorials. And as you say, there are alternatives.

@yihui
Copy link
Owner

yihui commented Dec 29, 2012

The easiest solution is to destroy the code chunk by adding an empty string to the chunk header:

    `r ''````{r foo}
    print('hello world!')
    ```

This is what I use when writing tutorials. Sorry for the trouble, but I think it makes sense to allow indented code blocks.

@jmarshallnz
Copy link

@yihui Apologies for reviving a dead issue, but is there a similar trick for displaying the source for an inline r computation, i.e.

The average speed was `r mean(cars$speed)`.

?

@yihui
Copy link
Owner

yihui commented Oct 1, 2014

@jmarshallnz Yes, the simplest solution is to insert a zero-width space between the first backtick and the little r. A slightly more trickier solution:

`` `r '\x60r mean(cars$speed)\x60'` ``

\x60 is the backtick character. Let me know if there is anything else that you need me to explain.

@jmarshallnz
Copy link

Thanks - that does the trick :)

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants