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 · 7 comments

Comments

4 participants
@jeromyanglim

jeromyanglim commented Dec 20, 2012

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

This comment has been minimized.

Show comment
Hide comment
@ramnathv

ramnathv Dec 20, 2012

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.

Contributor

ramnathv commented Dec 20, 2012

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

This comment has been minimized.

Show comment
Hide comment
@ramnathv

ramnathv Dec 20, 2012

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)

Contributor

ramnathv commented Dec 20, 2012

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

This comment has been minimized.

Show comment
Hide comment
@jeromyanglim

jeromyanglim Dec 20, 2012

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.

jeromyanglim commented Dec 20, 2012

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

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Dec 29, 2012

Owner

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.

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

This comment has been minimized.

Show comment
Hide comment
@jmarshallnz

jmarshallnz Oct 1, 2014

@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)`.

?

jmarshallnz commented Oct 1, 2014

@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

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Oct 1, 2014

Owner

@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.

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

This comment has been minimized.

Show comment
Hide comment
@jmarshallnz

jmarshallnz Oct 1, 2014

Thanks - that does the trick :)

jmarshallnz commented Oct 1, 2014

Thanks - that does the trick :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment