Sometimes code is an important part of an answer. Other times, code is interesting and potentially useful to others, e.g. how to construct a plot, but doesn't help the OP in answering their question. I try to append code for plots (partly as it frustrates me when I can't replicate or adapt for personal use the beautiful plots of others!) and if I find the code interesting, or OP expressed an interest, I'm likely to continue to do so, but for a workaday plot the temptation to "provide, but hide" is strong.
In the thread "Shall I attach pieces of code to my answers?", the concept of a "show/hide" button for code was marked as rejected. But the reception to the later thread "Do we need to hide some lengthy content in answers or questions?" seemed positive! I'm not satisfied with the suggested solution of linking to a repository elsewhere - eg potential link-rot issues - and the presence of scrollbars on long code extracts only alleviates such concerns, doesn't resolve them.
In an answer (it happens to be this one, but I think that's unimportant) I included some code for data and plotting at the bottom, then quickly (to avoid spamming the main page) re-edited it, deleting the code, but inserting a link to the code-containing revision by the graph. The result is that the code is stored on the main CV site, is accessible to interested readers, but does not clog up the answer.
In retrospect I am not sure whether this was a good idea. There was support in the "Shall I attach" thread for the inclusion of such code, even if peripheral to the main answer, and I'm basically using a bit of a hack to conceal it. It certainly wasn't a use that edit history was intended for. Moreover I don't know whether edit history links are considered stable in the very long run on Stack Exchange (I suspect so because I believe licensing here requires authorial attribution which is visible via history, but on the other hand there are occasional rumblings that old comments on SE sites will someday be purged, so just because material is hosted here doesn't necessarily mean it's here to stay). Is this a practice I should actively engage in, use sparingly, or avoid altogether?
<!-- language: lang-r -->
and leaving empty line (!) before the code block. If you writelanguage-all
then all subsequent blocks will also have highlighting. See here for more details. $\endgroup$