编译PDF时自定义错误信息 [英] Customize error message when compiling PDF
问题描述
鉴于以下R编织文件:
\documentclass{article}
\begin{document}
<<data>>=
opts_chunk$set(comment = NA) # omits "##" at beginning of error message
x <- data.frame(x1 = 1:10)
y <- data.frame()
@
<<output_x>>=
if (nrow(x) == 0) stop("x is an empty data frame.") else summary(x)
@
<<output_y>>=
if (nrow(y) == 0) stop("y is an empty data frame.") else summary(y)
@
\end{document}
正如预期的那样,最后一个块返回自定义消息的错误。编译的PDF看起来有点不同:
As expected, the last chunk returns an error with the custom message. The compiled PDF looks a little different:
Error: y is an empty data frame.
我希望这个文本只是
y is an empty data frame.
没有错误:
部分或红色颜色。我可以实现吗如何?
Without the Error:
part or the red color. Can I achieve this? How?
编辑:通过以下解决方法,我可以在模拟数据中使用:
I was able to make it in the mock data through the following workaround:
<<output_y>>=
if (nrow(y) == 0) cat("y is an empty data frame.") else summary(y)
@
但是,这并不适用于我的真实数据,因为我需要在那个时候停止这个功能。
However, that doesn't work with my real data, because I need the function to be stopped at that point.
推荐答案
虽然我不明白为什么不应该调用错误错误
,您可以自定义输出钩子错误
以删除错误:$ code code code code code $ c
$ b
Although I do not understand why an error should not be called Error
, you are free to customize the output hook error
to remove Error:
from the message:
library(knitr)
knit_hooks$set(error = function(x, options) {
knitr:::escape_latex(sub('^Error: ', '', x))
})
这篇关于编译PDF时自定义错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!