8

Rstudio changed how a code section is defined. In version 0.99.902 code sections had to have some text behind the hash symbol. But now in version 1.0.136 if there are 5 hashes in a row it will define a new section.

Is there anyway to make it go back to the old way of defining sections? It isn't a big deal except I would mark my sections with hashes above and below the name and now it is creating 3x as many sections.

Old version:

enter image description here

New version:

enter image description here

0

2 Answers 2

3

I don't know if there's a way to recover the old behavior, but you could use + instead. In addition, you can put this in a code snippet (if you haven't already). In Preferences, go to the Code tab, scroll to the bottom and click the Edit Snippets button. Then add something like the following:

snippet hd
  `r "# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ### HEAD ##########
  # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"`

Then, when you type hd followed by a tab (actually two tabs, since the first tab will bring up a few options that start with hd, but hd will be at the top, so you can just press tab twice) in your R script file, the following will appear:

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
### HEAD ##########
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Better yet, you can create a snippet that takes the heading text as an argument:

snippet hd
   `r paste("# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
   "### ", "${1:HEAD}", " ##########\n",
   "# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", sep="")`

Then, when you type hd followed by two tabs, the HEAD text will be highlighted and you can just type in your actual heading text.

2
  • Good tip about the snippets. I didn't know you could do that. I was debating just using a different symbol to avoid the extra sections, but that doesn't help my old code. Commented Mar 30, 2017 at 17:13
  • You could do a find and replace to change #######...###### to # ++++...+++++ whenever you open an old script. Painful, but at least it's only one operation and needs to be done only once for a given script. If you're handy with the command line, you could do this for all R scripts at once, preferably using regular expressions to capture and replace only "long" strings of #.
    – eipi10
    Commented Mar 30, 2017 at 17:16
1

Unfortunately, this behavior has changed between RStudio v0.98.1091, v0.99.903 and the current release v1.0.136.

In RStudio v0.98.1091, 'empty' headers such as ##### were recognized as section headers.

This behavior was briefly changed with v0.99.903, such that some initial text was required for these to be recognized as section headers. A number of users were unhappy as this effectively broke code folding for users who were explicitly using standalone ##### blocks to create sections.

Because of that, the behavior was reverted in RStudio v1.0.136, and so now standalone ##### blocks are again recognized as section headers.

3
  • Thanks for the response. Is there a post on Rstudio about this? Commented Mar 31, 2017 at 18:50
  • I don't think standalone sections with e.g. #### were ever explicitly documented, but because they worked they implicitly became part of how sections are created in RStudio. The closest thing I can find is this support article: support.rstudio.com/hc/en-us/articles/… Commented Mar 31, 2017 at 19:48
  • Where did you find the notes on the changes with each version? Commented Apr 2, 2017 at 0:52

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.