title: How to hide code in RMarkdown, with option to see it
body: <p>I'm writing an RMarkdown <a href="http://rpubs.com/RobinLovelace/3380" rel="noreferrer">document</a> in which I'd like to re-run some chunks (5 to 9).
  There's no need to display these chunks again, so I considered using </p>
  
  <pre><code>```{r echo=FALSE}
  </code></pre>
  
  <p>to make the rerun chunks invisible, as described in another <a href="https://stackoverflow.com/questions/13091112/hiding-the-r-code-in-rmarkdown-knit-and-just-showing-the-results">stackoverflow question</a>. This is fine, and outputs the desired results (improved fit of second iteration - see this solution implemented <a href="http://rpubs.com/RobinLovelace/3383" rel="noreferrer">here</a>). </p>
  
  <p>In an ideal world, however, the code would be expandable so the user could see exactly what's going on if they want to for educational purposes and clarity (e.g. see link to Greasemonkey solution <a href="https://stackapps.com/questions/2574/click-to-expand-code-regions">here</a>) rather than hidden as in my second rpubs example. The solution may look something like this, but with a shorter surrounding box to avoid distraction:</p>
  
  <pre><code>for (i in 1:nrow(all.msim)){ # Loop creating aggregate values (to be repeated later)
    USd.agg[i,]   &lt;- colSums(USd.cat * weights0[,i])
  }
  
  for (j in 1:nrow(all.msim)){
  weights1[which(USd$age &lt;= 30),j] &lt;- all.msim[j,1] /USd.agg[j,1] 
  weights1[which(USd$age &gt;= 31 &amp; USd$age &lt;= 50),j] &lt;- all.msim[j,2] /USd.agg[j,2] 
  weights1[which(USd$age &gt;= 51),j] &lt;- all.msim[j,3] /USd.agg[j,3] ## 
  }
  # Aggregate the results for each zone
  for (i in 1:nrow(all.msim)){
    USd.agg1[i,]   &lt;- colSums(USd.cat * weights0[,i] * weights1[,i])
  }
  # Test results 
  for (j in 1:nrow(all.msim)){
  weights2[which(USd$sex == "m"),j] &lt;- all.msim[j,4] /USd.agg1[j,4]  
  weights2[which(USd$sex == "f"),j] &lt;- all.msim[j,5] /USd.agg1[j,5] 
  }
  
  for (i in 1:nrow(all.msim)){
  USd.agg2[i,]   &lt;- colSums(USd.cat * weights0[,i] * weights1[,i] * weights2[,i])
  }
  
  for (j in 1:nrow(all.msim)){
  weights3[which(USd$mode == "bicycle"),j] &lt;- all.msim[j,6] /USd.agg2[j,6]  
  weights3[which(USd$mode == "bus"),j] &lt;- all.msim[j,7] /USd.agg2[j,7] 
  weights3[which(USd$mode == "car.d"),j] &lt;- all.msim[j,8] /USd.agg2[j,8]  
  weights3[which(USd$mode == "car.p"),j] &lt;- all.msim[j,9] /USd.agg2[j,9]
  weights3[which(USd$mode == "walk"),j] &lt;- all.msim[j,10] /USd.agg2[j,10]
  }
  weights4 &lt;- weights0 * weights1 * weights2 * weights3
  for (i in 1:nrow(all.msim)){
  USd.agg3[i,]   &lt;- colSums(USd.cat * weights4[,i])
  }
  # Test results 
  plot(as.vector(as.matrix(all.msim)), as.vector(as.matrix(USd.agg3)),
       xlab = "Constraints", ylab = "Model output")
  abline(a=0, b=1)
  cor(as.vector(as.matrix(all.msim)), as.vector(as.matrix(USd.agg3)))
  #rowSums(USd.agg3[,1:3]) # The total population modelled for each zone, constraint 1
  #rowSums(USd.agg3[,4:5])
  #rowSums(USd.agg3[,6:10])
  </code></pre>
  
  <p>I'm happy with the <code>echo=F</code> solution, but would be even happier with an expandable code snippet one.</p>
  
  <p><strong>Edit: all RPubs examples except the first have now been removed, to avoid clogging their excellent publication system with essentially the same document.</strong></p>