Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change in chunk message handling in new version of knitr

In previous versions of knitr, I was able to set the chunk option message = FALSE and it would both have the documented feature of preventing messages from going to the text document (markdown in this case) and would also have the undocumented feature of disabling the interception of messages, allowing me to capture messages within the chunk to reformat and output to markdown as I wished. With the new version of knitr (1.43), this no longer seems to work and thus has broken a fair amount of my report generation code. Setting message = FALSE now seems to result in messages being intercepted without allowing code within the chunk to capture them with functions like capture.output(). My workaround has been to write functions in chunks that I can use to capture messages in inline R code, where any messages generated seem to be left alone by knitr. This makes for pretty sloppy code and is less than convenient, and it seems to me like knitr should retain a feature allowing me to tell a chunk to ignore message and error reporting and let me do with them as I please in terms of reformatting them and sending them to the text document. I am not sure this would be considered a bug, but it does seem like an undocumented feature change (or at least I cannot find the documentation of the desired behavior).

Anyone know of a more predictable and stable way to prevent knitr from interfering with custom message and error management within chunks? (I know that I can use inline code, but for reasons I don't want to detail here, I would rather use chunks.)

In old versions of knitr, setting the chunk option message = FALSE used to allow me to capture messages within the chunk and format them for output myself. I was expecting this to continue to work with new versions of knitr.

like image 221
Robert Payn Avatar asked Dec 11 '25 11:12

Robert Payn


1 Answers

As noted in the comments of the original question, Konrad Rudolph suggested what I suspect is a robust solution to use withCallingHandlers rather than capture.output to intercept messages. I tried it and this seems to work fine in a knitr chunk.

See comments above, but I suspect this is because capture.output() tries to read the messages output stream (when argument type = "message"), but the withCallingHandlers() function does not care about the output streams and just watches for the message events directly.

like image 85
Robert Payn Avatar answered Dec 14 '25 01:12

Robert Payn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!