Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print to the console when using knitr?

Tags:

r

rstudio

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}

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.

I am using knitr 1.13 and 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.

like image 523
bwk Avatar asked Sep 01 '16 21:09

bwk


People also ask

How do I display markdown output?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How do you show output in R markdown in knitting?

Rendering output Better still, use the “Knit” button in the RStudio IDE to render the file and preview the output with a single click or keyboard shortcut (⇧⌘K).

How do I not print output in R markdown?

You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.

What does the knitr package do?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.


1 Answers

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

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

  • 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.

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

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}

The progress window reads:

Progress window

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

like image 178
CL. Avatar answered Sep 21 '22 14:09

CL.