r - How can I print to the console when using knitr? -


i trying print console (or output window) debugging purposes. example:

\documentclass{article}  \begin{document}  <<foo>>= print(getwd()) message(getwd()) message("error:") cat(getwd(), file=stderr()) not_a_command() # not throw error? stop("why doesn't throw error?") @   \end{document} 

i results in output pdf, problem have script not completing (so there no output pdf check), , i'm trying understand why. there appears no log file output if knitting doesn't complete successfully.

i using knitr 1.13 , rstudio 0.99.896.

edit: above code correctly output (and break) if change sweave, makes me think knitr issue.

this question has several aspects – , partly xy problem. @ core, question (as read it):

how can see what's wrong if knitr fails , doesn't produce output file?

  • in case of pdf output, quite compiling output pdf fails after error occurred, there still intermediate tex file. opening file may reveal error messages.
  • as suggested gregor, can run code in chunks line line in console (or chunk). however, may not reproduce problems, if related working directory or environment.
  • capture.output can used print debug information external file.
  • finally (as opposed earlier comment), is possible print on rstudio's progress window (or it's called): messages from hooks printed on progress window. basically, message must come knitr itself, not code knitr evaluates.

how can print debug information on progress window in rstudio?

the following example prints objects in environment after each chunk 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>>= <- 5 foo <- "bar" @  \end{document} 

the progress window reads:

progress window

of course, documents more or larger objects hook should adjusted selectively print (parts of) objects.


Comments