Convergence warnings missing from knit documents if fits wrapped in summary() - r

I use knitr/rmarkdown to document my analyses for others and myself.
The package I use, lme4, produces convergence warnings, which need to be documented.
If I run my model in the console, I get the warning right after, but the same warnings do not show inside the knit document, they end up in the Markdown console (i.e. after knitting is over).
It somehow depends on the top-level function that is being executed. To reproduce this, I tried creating a warning inside a function myself. This always showed in the Markdown.
While writing the question, it occured to me that I might play with options(warn). Setting it to 1 makes the warnings occur immediately in the Markdown console, but not in the knit document itself.
This at least fixes this problem: if I get too many warnings, I'm asked to execute warnings() but can't do so in the knitting parent environment.
Here's a reproducible script:
http://rpubs.com/rubenarslan/missing_convergence_warnings
I've figured out that the convergence warnings are only hidden if I wrap the model fitting into summary like so summary(m1 <- glmer(...)), not if I do m1 <- glmer(...); summary(m1). I borrowed the example and also found a way to get convergence warnings from the fit using this extractor fit#optinfo$conv$lme4$messages.
I can change my scripts now, but I don't really think it should be this way, I don't see a real difference between the two approaches, wrapping in summary is just more concise, the top-level function is knit in both cases.

Related

libstableR library unwanted output

I'm using libstableR library to calculate some estimators and function stable_fit_mle outputs some messages while running, namely tons of Minimizer warning: No error and occasionally also: FINAL ESTIMATED PARAMETER ARE NOT VALID: No error.
I managed to suppress those messages in my final document (compiled with R Sweave) with invisible(capture.output(...)) but those still clutter my console during compilation. Is there any way to stop those messages from appearing in the first place?
My main concern is to understand why stable_fit_mle is outputing these warnings (in oppose to similrar functions from libstableR package) and how to prevent this behaviour.

How to suppress not traditional warnings in R Console for Shiny

I have an R Shiny app. And I am making predictions from 10k xgboost models in the app. And the R console will show such error each time I make a prediction from a single model:
[23:13:24] WARNING: amalgamation/../src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
Some explanations about this warning can be found here: https://github.com/dmlc/xgboost/issues/4599
(I don't have time and can't rerun these models to fix the warnings now.)
Also, notice this is not a normal warning from the Shiny because this error is in black color and also showing in the specific form (see above).
I tried to use suppressWarnings function on my predict and other method such as the first reply in this thread: Suppress warning message in R console of shiny
But I still have the warnings. I think there are 10k warnings and my result will be shown on the Shiny interface after all the warnings are loaded, which is a big slow-down.
So anyone knows how to solve this? Or even a global way, such as suppress any output in R console while running a Shiny app.
Thanks in advance!!

Chunk with eval=FALSE still evaluates on R Sweave

I'm working on some education manuals in spanish for a course, so I'm making a Sweave document with some chunks and I'm trying to make an example of an error message. But first I need to show the souce of that error, so I'm using this code, since I don't want the code evaluating I'm using eval=FALSE:
<<eval=FALSE, error=TRUE,tidy=FALSE>>=
c(1,2 3)
#falta una coma
#
But the code is still evaluating and it's not letting me print the document, giving me this error message
(chunk 306) 5305:7: unexpected numeric constant
Your code is being parsed, not evaluated. If you have current versions of the knitr and evaluate, this should result in a warning in the knitr log, it won't stop the run. I'm using knitr 1.16 and evaluate 0.10.1 and things are fine. See knitr: knitting chunks with parsing errors for a bit more on this.
(BTW, I think you're using knitr, not Sweave. They're different. Sweave can't handle this. If you really are using Sweave, switch to knitr. The switch is not hard, and brings a lot of benefits.)

How to request an early exit when knitting an Rmd document?

Let's say you have an R markdown document that will not render cleanly.
I know you can set the knitr chunk option error to TRUE to request that evaluation continue, even in the presence of errors. You can do this for an individual chunk via error = TRUE or in a more global way via knitr::opts_chunk$set(error = TRUE).
But sometimes there are errors that are still fatal to the knitting process. Two examples I've recently encountered: trying to unlink() the current working directory (oops!) and calling rstudioapi::getVersion() from inline R code when RStudio is not available. Is there a general description of these sorts of errors, i.e. the ones beyond the reach of error = TRUE? Is there a way to tolerate errors in inline R code vs in chunks?
Also, are there more official ways to halt knitting early or to automate debugging in this situation?
To exit early from the knitting process, you may use the function knitr::knit_exit() anywhere in the source document (in a code chunk or inline expression). Once knit_exit() is called, knitr will ignore all the rest of the document and write out the results it has collected so far.
There is no way to tolerate errors in inline R code at the moment. You need to make sure inline R code always runs without errors1. If errors do occur, you should see the range of lines that produced the error from the knitr log in the console, of the form Quitting from lines x1-x2 (filename.Rmd). Then you can go to the file filename.Rmd and see what is wrong with the lines from x1 to x2. Same thing applies to code chunks with the chunk option error = FALSE.
Beyond the types of errors mentioned above, it may be tricky to find the source of the problem. For example, when you unintentionally unlink() the current directory, it should not stop the knitting process, because unlink() succeeded anyway. You may run into problems after the knitting process, e.g., LaTeX/HTML cannot find the output figure files. In this case, you can try to apply knit_exit() to all code chunks in the document one by one. One way to achieve this is to set up a chunk hook to run knit_exit() after a certain chunk. Below is an example of using linear search (you can improve it by using bisection instead):
#' Render an input document chunk by chunk until an error occurs
#'
#' #param input the input filename (an Rmd file in this example)
#' #param compile a function to compile the input file, e.g. knitr::knit, or
#' rmarkdown::render
knit_debug = function(input, compile = knitr::knit) {
library(knitr)
lines = readLines(input)
chunk = grep(all_patterns$md$chunk.begin, lines) # line number of chunk headers
knit_hooks$set(debug = function(before) {
if (!before) {
chunk_current <<- chunk_current + 1
if (chunk_current >= chunk_num) knit_exit()
}
})
opts_chunk$set(debug = TRUE)
# try to exit after the i-th chunk and see which chunk introduced the error
for (chunk_num in seq_along(chunk)) {
chunk_current = 0 # a chunk counter, incremented after each chunk
res = try(compile(input))
if (inherits(res, 'try-error')) {
message('The first error came from line ', chunk[chunk_num])
break
}
}
}
This is by design. I think it is a good idea to have error = TRUE for code chunks, since sometimes we want to show errors, for example, for teaching purposes. However, if I allow errors for inline code as well, authors may fail to recognize fatal errors in the inline code. Inline code is normally used to embed values inline, and I don't think it makes much sense if an inline value is an error. Imagine a sentence in a report like The P-value of my test is ERROR, and if knitr didn't signal the error, it will require the authors to read the report output very carefully to spot this issue. I think it is a bad idea to have to rely on human eyes to find such mistakes.
IMHO, difficulty debugging an Rmd document is a warning that something is wrong. I have a rule of thumb: Do the heavy lifting outside the Rmd. Do rendering inside the Rmd, and only rendering. That keeps the Rmd code simple.
My large R programs look like this.
data <- loadData()
analytics <- doAnalytics(data)
rmarkdown::render("theDoc.Rmd", envir=analytics)
(Here, doAnalytics returns a list or environment. That list or environment gets passed to the Rmd document via the envir parameter, making the results of the analytics computations available inside the document.)
The doAnalytics function does the complicated calculations. I can debug it using the regular tools, and I can easily check its output. By the time I call rmarkdown::render, I know the hard stuff is working correctly. The Rmd code is just "print this" and "format that", easy to debug.
This division of responsibility has served me well, and I can recommend it. Especially compared to the mind-bending task of debugging complicated calculations buried inside a dynamically rendered document.

Write markdown documents with R code that doesn't work, on purpose

I'm experimenting with using Markdown to write homework problems for a course that involves some R coding. Because these are homework sets, I intentionally write code that throws errors;. Is it possible to use Markdown to display R code in the code style without evaluating it (or to trap the errors somehow)?
If you're using R markdown, putting eval=FALSE in the chunk options should work. Or use try(). Or, if you're using knitr as well, I believe that the default chunk option error=FALSE doesn't actually stop the compilation when it encounters an error, but just proceeds to the next chunk (which sometimes drives me crazy).

Resources