<p>I occasionally run the (R) code in people's answers, so I think about it from that perspective.  I don't include the R console's command prompt, <code>&gt;</code>, because when you copy and paste code you have the added hassle of taking all that out before you can run it.  I also don't like code in many separate small chunks with text in between, because it's more of a hassle to copy and paste.  Consider:</p>
<blockquote>
<p>First I did</p>
<pre><code>stuff
</code></pre>
<p>Then I did</p>
<pre><code>other stuff
</code></pre>
<p>And then I fit the model</p>
<pre><code>model
</code></pre>
<p>which yields output</p>
<pre><code>output
</code></pre>
<p>but notice that to get what you want, you have to process the output</p>
<pre><code>process
</code></pre>
<p>then you can get what you want by computing</p>
<pre><code>extra computations
</code></pre>
<p>so now you can get your desired output</p>
<pre><code> output
</code></pre>
</blockquote>
<p>I understand the idea of narrative flow, but this is really tedious to copy and paste if you want to try playing with the code yourself.</p>
<p>I prefer to put the code into fewer, longer code blocks to the extent possible.  When small comments are needed to discuss moving from one step to the next, I use code-comments.  (I also try to use comments to make the code as transparent as possible for people who may not be very fluent in R, and I use only base R, use <code>=</code> instead of <code>&lt;-</code>, etc.)  Of course, showing the output is important to the answer, so I do, but I comment it out.</p>
<p>Here is an example code block from a recent <a href="https://stats.stackexchange.com/a/374500/7290">answer</a> of mine:</p>
  
<pre><code>set.seed(8649)     # this makes the example exactly reproducible
N      = 10        # this is how much data I'll generate
latent = rnorm(N)  # this is the actual latent variable I want to be measuring

##### generate latent responses to items
item1 = latent + rnorm(N, mean=0, sd=0.2)  # the strongest correlate
item2 = latent + rnorm(N, mean=0, sd=0.3)
item3 = latent + rnorm(N, mean=0, sd=0.5)
item4 = latent + rnorm(N, mean=0, sd=1.0)
item5 = latent + rnorm(N, mean=0, sd=1.2)  # the weakest

##### convert latent responses to ordered categories
item1 = findInterval(item1, vec=c(-Inf,-2.5,-1, 1,2.5,Inf))  # fairly unbiased
item2 = findInterval(item2, vec=c(-Inf,-2.5,-1, 1,2.5,Inf))
item3 = findInterval(item3, vec=c(-Inf,-3,  -2, 2,3,  Inf))  # middle values typical
item4 = findInterval(item4, vec=c(-Inf,-3,  -2, 2,3,  Inf))
item5 = findInterval(item5, vec=c(-Inf,-3.5,-3,-1,0.5,Inf))  # high ratings typical

##### combined into final scale
manifest = round(rowMeans(cbind(item1, item2, item3, item4, item5)), 1)
manifest
# [1]  3.4  3.6  3.4  3.8  2.6  3.4  3.2  2.0  3.8  3.2
round(latent, 1)
# [1]  1.3  0.6  0.2  1.0 -1.5  0.1  0.4 -2.5  2.3 -0.3
cor(manifest, latent)
# [1] 0.9280074
</code></pre>
<hr />
<p>There was also some discussion about how to get syntax highlighting.  If the <code>[r]</code> tag is on a thread, syntax highlighting (for R) should automatically occur.  Before any code block, you can initiate, or override, local syntax highlighting with <code>&lt;!-- language: lang-r --&gt;</code> on a prior line.  If you want that to span several code blocks within your answer, use <code>&lt;!-- language-all: lang-r --&gt;</code>, instead.  Note that <a href="https://meta.stackexchange.com/q/353983/187282">SE is switching the engine it uses for formatting</a>.  The new system will let you set off a code block without indenting by using ``` before and after the block, and you can turn on syntax highlighting for that block by appending <code>lang-r</code> to the set of opening backticks.</p>
