0

I have a two-level ordered list, and the output is identical to this post. My output is fine and looks how it should. My issue is the normal 'highlighting' of chunks disappears with 8 spaces, and the RMD source no longer 'recognizes' the chunks as such. The blank lines before and after the chunks are also indented (I found that I had to do this to get the desired output).

To illustrate this, I'm sharing the image below. The highlight is missing, and that same cursor position in other chunks gives a little button next to line:character in the bottom left rather than '(Top Level)'. If I bring the chunk indention in (from 8 spaces to 4) the code no longer lines up with the list in the output.

enter image description here

Is there any way to remedy this? Haven't had any luck searching online or going through the documentation. (If interested, the example comes from ISLR, Section 2.4, Exercise 8.)

Community
  • 1
  • 1
J.M.
  • 257
  • 1
  • 2
  • 13
  • I suspect the issue is that the backtics ("```") which demark the R code aren't at the begining of the lines in which they appear – Jthorpe Mar 29 '16 at 20:37
  • I tried that, but to no avail. Backing the eight spaces to four causes the alignment of the list to be off (that is, the chunks appear indented once while list items are indented twice). In earlier chunks (indented four spaces) that is not an issue. It's only with eight spaces that it becomes an issue. – J.M. Mar 29 '16 at 20:41
  • [This post](http://meta.stackexchange.com/questions/3792/how-to-nest-code-within-a-list-using-markdown) may be of use. – Jthorpe Mar 29 '16 at 20:45
  • Thanks for sharing - I came across that link earlier. The thing is, the output is exactly what I want. My issue is that RMD (or {knitr}?) is not highlighting the chunk, and further, does not 'recognize' it as a chunk (in RMD) at eight spaces. Four spaces is fine, eight is not. Just trying to figure out how to remedy it... Is this an option I need to tweak somewhere? – J.M. Mar 29 '16 at 20:55

2 Answers2

3

Ok, got it working. For some reason hitting tab (four spaces) twice in R Studio causes the issue above. I suspect it has something to do with R Studio rather than R Markdown or {knitr}. The solution is this response by @Yihui, and relies on using the indent parameter. That will keep the highlight and recognition of the chunk as well as keeping everything aligned in the output.

I never thought to use the indent parameter as it's not listed in the {knitr} chunk options in the current R Markdown Reference Guide (here), and is only mentioned passively at the end of the Code Description section in the {knitr} chunk options (here).

Community
  • 1
  • 1
J.M.
  • 257
  • 1
  • 2
  • 13
2

I find that if I keep the indention of the code at one more indent level that the corresponding text, everything renders nicely:

1. One level of indention
    * Two levels of indention
        * Three levels of indention
            * Four levels of indention
                ```{r,eval=FALSE}
                Some("R code")
                ```

enter image description here

Jthorpe
  • 9,756
  • 2
  • 49
  • 64
  • thanks for all of the help and thoughts on this. I updated the image in the original post, and I hope it better reflects my question / issue. – J.M. Mar 29 '16 at 22:52
  • For this to work you need to indent using nested [markdown lists](https://daringfireball.net/projects/markdown/syntax#list) so that the markdown compiler can detect the current level of indention. In your example, you are using `(a)`,`(b)`,`i.` and `ii.` which are not markdown list identifiers. – Jthorpe Mar 29 '16 at 23:31
  • Apologies for the delay - I downloaded the latest version of R Studio, and ensured all packages were up to date. Believe I am using the correct syntax for nested markdown lists, and the issue still persists. Indenting (at four spaces) twice causes the same issue as in the OP [link](https://drive.google.com/file/d/0B1VN9f5TVJC2ekZjSmptV0Q5WFk/view?usp=sharing). Same issue even if using the asterisk for the second level. Any thoughts? – J.M. Apr 01 '16 at 16:14
  • Quick check: Does the above example work? If not, can you post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Jthorpe Apr 01 '16 at 16:37
  • Copy pasting the above example into R Studio produces the same output, but the highlighting issue still persists [here](https://drive.google.com/file/d/0B1VN9f5TVJC2T28tX3lXODFxT0U/view?usp=sharing). I suspect it's an R Studio issue rather than one in R Markdown or {knitr}. While I was not able to directly solve the problem at hand, I did manage a workaround with the `indent` chunk option. – J.M. Apr 01 '16 at 16:50