Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show (diagnostic) messages in render pane while rendering Rmarkdown file

With one of the recent updates of knitr the output in the render pane in RStudio became less cluttered in so far that only the information about the current chunk is shown, instead of adding all rendering information to the log.

However, since this change I am struggling to add my timing hooks properly.

Let's take this sample document:

---
title: "TimeMe"
output: html_document
---

```{r setup, include=FALSE}
message(paste("Compiling started at:", Sys.time()))
start_compiling <- Sys.time()
chunk_timings <- data.frame(chunk = character(0), timing = numeric(0))
options(warn = 1)

knitr::opts_chunk$set(warning = FALSE, 
                      message = FALSE)

knitr::knit_hooks$set(time_it = local({
   now <- NULL
   function(before, options) {
      if (before) {
         # record the current time before each chunk
         now <<- Sys.time()
      } else {
         # calculate the time difference after a chunk
         res <- difftime(Sys.time(), now, units = "secs")
         # return a character string to show the time
         chunk_timings <<- rbind(
            chunk_timings,
            data.frame(chunk = options$label, timing = res)
         )
         message(glue::glue("# {options$label}: {round(res, 2)}s"))
      }
   }
}))
```

```{r libs}
library(glue)
library(dplyr)
```


```{r wait-1}
Sys.sleep(1)
```

```{r wait-2}
Sys.sleep(2)
```

```{r wait-5}
Sys.sleep(5)
```


```{r finis-artis}
local({
   n_slow <- 5L
   res <- difftime(Sys.time(), start_compiling, units = "secs")
   message(glue("Total Compiling Time: {round(res, 2)}s"))
   slowest <- capture.output(chunk_timings %>% 
                                as_tibble() %>% 
                                arrange(desc(timing)) %>% 
                                slice(1:n_slow)) %>% 
      paste(collapse = "\n")
   message(glue("TOP {n_slow} Slowest Chunks:\n{slowest}"))
   message(glue("Compiling finished at: {Sys.time()}"))
})
```

If you render it you see the following Render output in the render pane in Rstudio:

Render Output Gif of Rtstudio with no timing

You will observe that the message form the last chunk (finis-artis) is not shown in the render pane.

This is strange because if you turn on the timing by changing the default options of the chunks

knitr::opts_chunk$set(warning = FALSE, 
                      message = FALSE,
                      time_it = TRUE)

You see at least that the messages from the previous chunks are added to the render pane (though distorting the new cleaned log somehow):

Render Output Gif of Rtstudio with timing

Regardless of what I try, I cannot display the message from the last chunk in the render pane.

Ideally, I'd like to be able to send any message to the render pane (at any time, but with the new look and feel of the render output it makes sense to do that only at the end)

like image 366
thothal Avatar asked Sep 16 '25 13:09

thothal


1 Answers

With knitr >= v1.42 and evaluate >= v0.20, you need to set the chunk option message = NA to send messages to stderr. This is equivalent to message = FALSE in previous versions of knitr and evaluate.

like image 191
Yihui Xie Avatar answered Sep 18 '25 05:09

Yihui Xie