3

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

1 Answer 1

1

From knitr v1.2.11 and above, the RStudio style code headers are supported consistently in knitr. The rule is basically # ---- label:

  • one or more hashes # in the beginning
  • followed by at least four dashes ----
  • followed by the chunk label
  • and optionally followed by any number of dashes

This is supported in both read_chunk() and purl(), i.e., this style of comments is used in both importing and exporting code in knitr.

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

# ---- chunk-label -----------------------------

knitr 1.2.11 is a development version on Github, which will become 1.3 eventually on CRAN.

1
  • with version 1.20.8 this is not working for me. # ---- chunk-label ----------------------------- is ignored in the output, as a normal # comment. Any ideas?
    – fry
    Jul 19, 2018 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.