title: Adding titles inside loop/function of knitr spin script
body: <p>I have an r script that I'd like to use to generate an HTML report using knitr spin.</p>
  
  <p>Much of the code that runs is within loops/functions.  How can I generate titles within the document for content that is created within those functions/loops?</p>
  
  <p>I have tried a couple different ways and nothing has worked so far.  A reproducible example of something that won't have any titles when spun is given below:</p>
  
  <pre><code>#' ---
  #' title: "My Doc"
  #' author: "ME"
  #' date: "January 28, 2017"
  #' ---
  library(ggplot2)
  myFun = function(){
    for(i in seq(from=1,to=3, by=1)){
      #' ## This title won't show up
      cat("\n## Nor will this one\n")
      plot = ggplot(aes(x=mpg,y=cyl),data=mtcars) + geom_point()
      print(plot)
    }
  }
  </code></pre>