使用knitr时如何打印到控制台? [英] How can I print to the console when using knitr?

查看:88
本文介绍了使用knitr时如何打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印到控制台(或输出窗口)以进行调试.例如:

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}

我在输出PDF中得到结果,但是我的问题是我的脚本没有完成(因此没有要检查的输出PDF),并且我试图理解原因.如果编织未成功完成,则似乎也没有日志文件输出.

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.

我正在使用knitr 1.13和Rstudio 0.99.896.

I am using knitr 1.13 and Rstudio 0.99.896.

编辑:如果我更改为Sweave,上述代码将正确输出(并中断),因此使我认为这是一个编织问题.

EDIT: The above code will correctly output (and break) if I change to Sweave, so that makes me think it is a knitr issue.

推荐答案

这个问题有几个方面–部分是

This question has several aspects – and its partly a XY problem. At the core, the question is (as I read it):

如果knitr失败并且不生成输出文件,我该怎么办?

How can I see what's wrong if knitr fails and doesn't produce an output file?

  • 对于PDF输出,经常会在发生错误后编译输出PDF失败,但是仍然存在中间TEX文件.打开此文件可能会显示错误消息.
  • 由Gregor建议,您可以在控制台(或按块)中逐行运行代码.但是,这可能不会重现所有问题,尤其是与工作目录或环境有关的问题.
  • capture.output可用于将调试信息打印到外部文件.
  • 最后(相对于我之前的评论),可以 在RStudio的进度窗口上打印(或称其为):来自钩子的消息 将被打印在进度窗口.基本上,消息必须来自knitr本身,而不是来自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.
    • 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.
    • capture.output can be used to print debug information to an external file.
    • 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 code knitr evaluates.
    • 如何在RStudio的进度窗口中打印调试信息?

      How can I print debug information on the progress window in RStudio?

      以下示例使用debug = TRUE在每个块之后打印环境中的所有对象:

      The following example prints all objects in the environment after each chunk with 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}
      

      进度窗口显示为:

      当然,对于具有更多或更大对象的文档,应调整挂钩以选择性地打印(部分)对象.

      Of course, for documents with more or larger objects the hook should be adjusted to selectively print (parts of) objects.

      这篇关于使用knitr时如何打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆