Pretty printing

Print highlighted source code of a function

2012-02-11

In the knitr mailing list there was a question on how to print the highlighted source code of functions.

Of course it is fairly easy to print the source code of a function, as long as it is available to the user – just write the name of the function, and R will print its source when this name is evaluated, but the problem is the printed result is treated as text output from a chunk, so it is put in the verbatim environment, and nothing is highlighted.

Below is a complete demo showing how to use a chunk hook extract the source code of a function using dump() and insert it into knitr.