Rmarkdown & {knitr}: code chunk highlights in lists? - r

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

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

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")
```

Related

Headings not working in my R markdown file

I recently finished my R notebook, and noticed something once I decided to knit the markdown file. None of the headings or bullet points were working! I had to install XQuartz to finish knitting the markdown file since I have a mac. Is this something that's normal or can I fix this?
I haven't seen your rmarkdown, but I suspect that you need to add a blank line above each heading. For example, putting a heading right after a list (or any other structure) sometimes results in problems:
1. First item
2. Second item
3. Third item
# First level heading
However, adding a blank line before each heading usually fixes the problem:
1. First item
2. Second item
3. Third item
# First level heading

Markdown output not fitting column

I'm writing an article in RStudio (global setting is using knitr) trying to make the output generated by a chunk of code fit the specification of the IEEE jornal, butthe output will wonder off the column (text is organized in two columns).
I've tried specifying the out.width of the code chunk with the column width (out.width="\columnwidth") but it doesn't seem to do much. I've also tried specifying the same parameter with "\linewidth" and "\textwidth" but still I get the same output to the pdf. I've also tried scaling down the size by puting in "%10" and "1%" but no sucess.
<>=
levels(classObito$estado_fisico)
#
Expected the output of the code chunk to fit into the column, but it wonders off the page or to the other column.
Solved with adding option(width="50") inside the code chunk

Line break in R Markdown documents without two spaces

I am trying to use R Markdown documents for report writing in a way I used Word before for a couple of months. While RStudio/Markdown is generally great, there is one thing that keeps embarrassing me: the need to input two spaces to force a line break. My whole life related to writing on the computer screen is a life when pressing Enter means breaking the line. Moreover, I see in the editor that line is broken and new paragraph starts, but after the compilation of Rmd-file I see a wall of text, without line breaks, and have to go back and check whether all line endings have two spaces on their end. It is awful. I understand that maybe I need to develop a habit to input two spaces (which does not seem intuitive), but it is hard.
Is there a way to enforce Markdown parser in RStudio count end of the line in Rmd document as a line break, so I can avoid entering two spaces to force a line break? Maybe, there are workarounds, like RStudio addins or something else? I have searched these on the Internet but has not found any.

Edit text/comments of rmarkdown and knitr reports without rerunning code

I love knitr & rmarkdown, but I often find myself in situations where I have a lengthy report that takes some nontrivial amount of time to run. After it's generated, I notice my inevitable typos in text. However, re-knitting everything to just fix a couple typos (just in text, not code) takes a long time and seems avoidable. I was about to start taking a hack at developing my own solution to this, but I'm thinking it's the kind of thing that could already have a mature solution which would likely be more robust than the one I'd build.
I'm wondering if there is solution out there within knitr or third party that would allow me to edit just the text of my reports without rerunning code, generating plots and outputs etc. I know, I can simply edit the generated html text, but then those changes must be replicated in the R/Rmd code that generated it, or they get out of sync. I'm envisioning a function like this:
argument 1: the R/Rmd script with text edits (no code changes)... perhaps a warning is generated when code chunks change
argument 2: the html output file from the last time the R script in argument was knitted without the text edits.
return: the html report (argument 2) updated with the comments in the R/Rmd script (argument 1).
I use the cache option sometimes for large datasets. I toggle eval and echo on and off when developing if I'm just working on the text of my report. However, I'm looking for a function that would take care of all this for me, so one doesn't have to mess with the code and chunk options to make small edits to text.
Here's an interim solution that lets you retain the speed of making changes directly to the rendered text, but you have to do a little work after you're done making changes.
Assuming the following files:
input.knitr is the original Knitr file with text and code integrated.
output.html is the resulting HTML code that has been rendered by Knitr.
Consider making direct text edits to output.html and then running something like Meld visual merge tool:
meld output.html input.knitr
Then manually select the edits in output.html that are new and should be fixed in the original source input.knitr. Tools such as Meld do a pretty good job of aligning the texts so that the chunks and knitted output will appear as large "changes" that, in practice, you would ignore. You would focus on the small changes in the non-chunk sections.

How can I retain the initial white space in a line when writing Rd documentation?

In conjunction with trying to find a solution for myself in regards to this question, I find myself plunged into trying to write valid Rd markup. What I want is to add a section named Raw Function Code and put the code of the function under it. I've achieved limited success in this regard by writing a script to modify the Rd files to include
\section{Raw Function Code}{\code{
# some piece of R script will eventally provide this part of the text
}}
However, even if I manually properly spaced text into the .Rd file (using either spaces or tabs), the initial white space of each line seems to get stripped away leaving an undesirable looking function. I've noticed that if I provide a starting character before the white space the white space is retained. However, I did not want to provide a starting character because I'd like people to be able to copy and paste directly from the produced PDF.
I have reviewed parseRd and I know there are three types of text LaTeX-
like, R-like, and verbatim. I have tried to put my function code in \code and in \verb and neither seemed to yield the desired results. What can I do to hold onto my initial white space?
The \section macro contains LaTeX type of text, but as you want to write code, you could use \synopsis macro, i.e.
\synopsis
# some piece of R script will eventally provide this part of the text
}
There is one problem with this though; you cannot give name to this section, it is automatically named as another usage section. Same thing could be achieved by using \examples macro, but now the name of the section is Examples, which is probably even more dubious (not to mention that you probably already have Examples section).
It isn't possible without modifying the usage or examples sections of your Rd code. See Hemmo's answer for a usable workaround. It produces text in the verbatim mode which is sub-optimal, but far better than nothing.
(This answer is set community Wiki in case this state of affairs changes. This result is current as of R-2.15.1)
If you want a super hacky way to do it, you can use \Sexpr to make zero width characters and add spaces between them:
#' first line \cr
#'\Sexpr{"\u200B"} \Sexpr{"\u200B"} \Sexpr{"\u200B"} \Sexpr{"\u200B"} indented line
A warning however - your package will build fine, but R CMD CHECK will throw a fit.

Resources