I can find no way to insert syntactically acceptable RStudio style folds into an external R code file that is set up for use from a knitr document. Or am I missing something. There are several ways this might be done: 1) Allow a code header such as:
## @knitr Q1 ----
or perhaps
## @knitr 'Q1' ----
2) Fold every code chunk (this would be a change in RStudio), but this is not as general as I would ideally like.
3) Allow the inclusion of some kind of comment line in code files that would indicate a fold. I have not been able to find a way to do this that does not add the comment line to the previous code chunk.
[Since initially posting this, I have noticed that the arguments 'from' and 'to' in read_chunk() can be regular expressions that specify start and from character strings for code chunks. So this gives one way to allow the insertion of comment lines that can specify folds. It would be nice however to be able to use one or more of mechanisms 1-3 above.]
From
knitr
v1.2.11 and above, the RStudio style code headers are supported consistently inknitr
. The rule is basically# ---- label
:#
in the beginning----
This is supported in both
read_chunk()
andpurl()
, i.e., this style of comments is used in both importing and exporting code inknitr
.For RStudio to support code-folding, however, you will have to add at least four dashes to the end of the comment header, e.g.,
knitr
1.2.11 is a development version on Github, which will become 1.3 eventually on CRAN.