在knitr新版本中,块消息处理的变化。

huangapple go评论85阅读模式
英文:

Change in chunk message handling in new version of knitr

问题

在早期版本的 knitr 中,我可以设置代码块选项 message = FALSE,它既能阻止消息进入文本文档(在这种情况下是 markdown),还能禁用消息拦截的未记录特性,允许我在代码块中捕获消息并按我希望的方式重新格式化并输出到 markdown。但是,使用新版本的 knitr(1.43)似乎不再起作用,因此破坏了我相当多的报告生成代码。现在设置 message = FALSE 似乎会导致消息被拦截,而不允许代码块内的代码使用诸如 capture.output() 等函数捕获它们。我的解决方法是在代码块中编写函数,我可以在其中捕获内联 R 代码生成的消息,这些消息似乎不会被 knitr 干扰。这导致代码变得混乱不堪,不太方便,而且在我看来,knitr 应该保留一个允许我告诉代码块忽略消息和错误报告并让我自行重新格式化并将它们发送到文本文档的功能。我不确定这是否被认为是一个 bug,但它似乎是一个未记录的功能更改(或者至少我找不到所需行为的文档)。

有人知道如何更可靠和稳定地防止 knitr 干扰代码块内自定义消息和错误管理的方法吗?(我知道我可以使用内联代码,但出于某些原因,我不想在这里详细说明,我宁愿使用代码块。)

在旧版本的 knitr 中,设置代码块选项 message = FALSE 以前允许我捕获代码块内的消息并自行格式化输出。我希望这个功能在新版本的 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.

答案1

得分: 1

Konrad Rudolph建议使用withCallingHandlers而不是capture.output来拦截消息,这似乎是一个强大的解决方案。我尝试了一下,在knitr块中似乎可以正常工作。

请查看上面的评论,但我怀疑这是因为capture.output()尝试读取消息输出流(当参数type = "message"时),但withCallingHandlers()函数不关心输出流,而是直接监听消息事件。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 21:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定