Rules by which RStudio sets Headings
Check out Code Folding and Sections:


  Code sections allow you to break a larger source file into a set of
  discrete regions for easy navigation between them. Code sections are
  automatically foldable—for example, the following source file has
  three sections (one expanded and the other two folded):
  
  To insert a new code section you can use the Code -> Insert Section
  command. Alternatively, any comment line which includes at least four
  trailing dashes (-), equal signs (=), or pound signs (#) automatically
  creates a code section. For example, all of the following lines create
  code sections:
  
  # Section One ---------------------------------
   # Section Two =================================
   ### Section Three ############################# 
  
  Note that as illustrated above the line can start with any number of
  pound signs (#) so long as it ends with four or more -, =, or #
  characters.


(highlights by myself)