I am trying to print to the console (or the output window) for debugging purposes. For example:
我正在尝试打印到控制台(或输出窗口)以进行调试。例如:
\documentclass{article}
\begin{document}
<<foo>>=
print(getwd())
message(getwd())
message("ERROR:")
cat(getwd(), file=stderr())
not_a_command() # Does not throw an error?
stop("Why doesn't this throw an error?")
@
\end{document}
I get the results in the output PDF, but my problem is I have a script that is not completing (so there is no output PDF to check), and I'm trying to understand why. There also appears to be no log file output if the knitting doesn't complete successfully.
我在输出PDF中得到了结果,但我的问题是我有一个未完成的脚本(所以没有输出PDF来检查),我试图理解为什么。如果编织未成功完成,似乎也没有日志文件输出。
I am using knitr 1.13 and Rstudio 0.99.896.
我正在使用knitr 1.13和Rstudio 0.99.896。
EDIT: The above code will correctly output (and break) if I change to Sweave, so that makes me think it is a knitr issue.
编辑:如果我改为Sweave,上面的代码将正确输出(并打破),这使我认为这是一个knitr问题。
1 个解决方案
#1
6
This question has several aspects – and its partly a XY problem. At the core, the question is (as I read it):
这个问题有几个方面 - 部分是XY问题。核心问题是(正如我所读到的):
How can I see what's wrong if
knitr
fails and doesn't produce an output file?如果knitr失败并且不生成输出文件,我怎么能看到错误?
- In case of PDF output, quite often compiling the output PDF fails after an error occurred, but there is still the intermediate TEX file. Opening this file may reveal error messages.
- 在PDF输出的情况下,经常在发生错误后编译输出PDF失败,但仍然存在中间TEX文件。打开此文件可能会显示错误消息。
- As suggested by Gregor, you can run the code in the chunks line by line in the console (or by chunk). However, this may not reproduce all problems, especially if they are related to the working directory or the environment.
- 正如Gregor所建议的那样,您可以在控制台(或块)中逐行运行块中的代码。但是,这可能无法重现所有问题,特别是如果它们与工作目录或环境相关。
-
capture.output
can be used to print debug information to an external file. - capture.output可用于将调试信息打印到外部文件。
- Finally (as opposed to my earlier comment), it is possible to print on RStudio's progress window (or however it's called): Messages from hooks will be printed on the progress window. Basically, the message must come from
knitr
itself, not from the codeknitr
evaluates. - 最后(与我之前的评论相反),可以在RStudio的进度窗口上打印(或者它被调用):来自挂钩的消息将打印在进度窗口上。基本上,消息必须来自knitr本身,而不是来自编码器编码评估。
How can I print debug information on the progress window in RStudio?
如何在RStudio中的进度窗口上打印调试信息?
The following example prints all objects in the environment after each chunk with debug = TRUE
:
以下示例使用debug = TRUE在每个块之后打印环境中的所有对象:
\documentclass{article}
\begin{document}
<<>>=
knitr::knit_hooks$set(debug = function(before, options, envir) {
if (!before) {
message(
paste(names(envir), as.list(envir),
sep = " = ", collapse = "\n"))
}
})
@
<<debug = TRUE>>=
a <- 5
foo <- "bar"
@
\end{document}
The progress window reads:
进度窗口显示:
Of course, for documents with more or larger objects the hook should be adjusted to selectively print (parts of) objects.
当然,对于具有更多或更大对象的文档,应该调整钩子以选择性地打印(部分)对象。
#1
6
This question has several aspects – and its partly a XY problem. At the core, the question is (as I read it):
这个问题有几个方面 - 部分是XY问题。核心问题是(正如我所读到的):
How can I see what's wrong if
knitr
fails and doesn't produce an output file?如果knitr失败并且不生成输出文件,我怎么能看到错误?
- In case of PDF output, quite often compiling the output PDF fails after an error occurred, but there is still the intermediate TEX file. Opening this file may reveal error messages.
- 在PDF输出的情况下,经常在发生错误后编译输出PDF失败,但仍然存在中间TEX文件。打开此文件可能会显示错误消息。
- As suggested by Gregor, you can run the code in the chunks line by line in the console (or by chunk). However, this may not reproduce all problems, especially if they are related to the working directory or the environment.
- 正如Gregor所建议的那样,您可以在控制台(或块)中逐行运行块中的代码。但是,这可能无法重现所有问题,特别是如果它们与工作目录或环境相关。
-
capture.output
can be used to print debug information to an external file. - capture.output可用于将调试信息打印到外部文件。
- Finally (as opposed to my earlier comment), it is possible to print on RStudio's progress window (or however it's called): Messages from hooks will be printed on the progress window. Basically, the message must come from
knitr
itself, not from the codeknitr
evaluates. - 最后(与我之前的评论相反),可以在RStudio的进度窗口上打印(或者它被调用):来自挂钩的消息将打印在进度窗口上。基本上,消息必须来自knitr本身,而不是来自编码器编码评估。
How can I print debug information on the progress window in RStudio?
如何在RStudio中的进度窗口上打印调试信息?
The following example prints all objects in the environment after each chunk with debug = TRUE
:
以下示例使用debug = TRUE在每个块之后打印环境中的所有对象:
\documentclass{article}
\begin{document}
<<>>=
knitr::knit_hooks$set(debug = function(before, options, envir) {
if (!before) {
message(
paste(names(envir), as.list(envir),
sep = " = ", collapse = "\n"))
}
})
@
<<debug = TRUE>>=
a <- 5
foo <- "bar"
@
\end{document}
The progress window reads:
进度窗口显示:
Of course, for documents with more or larger objects the hook should be adjusted to selectively print (parts of) objects.
当然,对于具有更多或更大对象的文档,应该调整钩子以选择性地打印(部分)对象。