There’s an unfortunate bug in rstan https://github.com/stan-dev/rstan/issues/49, whereby messages from the sampler escape through, and stick in the output. This occurs when using knitr to compile an Rnw document. The following is an example of such output:

##
##  Elapsed Time: 0.17787 seconds (Warm-up)
##                0.157655 seconds (Sampling)
##                0.335525 seconds (Total)
##
##
##  Elapsed Time: 0.17379 seconds (Warm-up)
##                0.157356 seconds (Sampling)
##                0.331146 seconds (Total)
##
##
##  Elapsed Time: 0.16872 seconds (Warm-up)
##                0.174496 seconds (Sampling)
##                0.343216 seconds (Total)
##
##
##  Elapsed Time: 0.219746 seconds (Warm-up)
##                0.165053 seconds (Sampling)
##                0.384799 seconds (Total)

I’d prefer not to have all this output floating around in my typeset documents, thank you very much! So, until the developers of stan/rstan fix the output issue, it’s going to have to be brute force, I’m afraid! Although in this case, brute force is very quick!

So, we need to edit the tex file that knitr produces. We could obviously do that by hand, although it’s tedious, not to mention not very reproducible.

(Note: everything below I’m doing on a mac. It is only tested on a mac, so ymmv.)

In my analysis pipeline, I use Makefiles to do all my heavy lifting. So, we need a simple way to take the tex output, and remove the offending rstan output.

Enter the mighty sed. sed is the stream editor, and mac uses BSD sed. With sed, we can search through a file using regular expressions, and replace them with appropriate replacements.

knitr places the output inside a kframe environment, so it’s this stuff we want to remove. What I’m about to show you will remove ALL kframe environments however, so if you do have verbatim output you want to keep, you’ll have to edit this. Also, I’m assuming that you have the rstan sampling statements in your Rnw file.

Let’s say you have the following rule in your Makefile to generate the tex file (where the rule changes directory to report and runs knitr::knit() on the file output.tex):

report/output.tex: report/output.Rnw
cd $(<D); \ echo "knitr::knit('$(<F)')" | R --no-save --no-restore


Leaving it at this, you’ll get the aforementioned output creeping in to the tex file. Let’s change it to this:

report/output.tex: report/output.Rnw
cd $(<D); \ echo "knitr::knit('$(<F)')" | R --no-save --no-restore
sed -i .bk -E $$'s/\\end{kframe}/&\\\n/g;' (@F); \ sed -i .bk '/\\begin{kframe}/,/\\end{kframe}/d;' (@F); \ rm *.bk  The -i .bk tells sed to store backups (required on BSD sed) with the extension .bk; the ‘$$’ tells make not to interpret the string; and the stuff in the single quotes is the magic: 's/searchstring/replacestring/g;'. Here we’re replacing the \end{kframe} with \end{kframe} and a newline. We need the newline, as knitr places the \end{kframe} on the same line as the following environment. We need the newline, because of the next sed command: '/startsearchstring/,/endsearchstring/d' replaces all text between (and including) the lines starting with ‘startsearchstring’ and ending with ‘endsearchstring’. Finally we delete the backup.

And that’s it!

\$ make report/output.tex

And you’re good to go!