'How can I get the order of plots correct in an rmd? [How can I control the print-time of messages?]

As the minimal reproducible example below shows, plot 2 appears too late. How can I fix this?

It seems, the message screws everthing up. Most likely related: this and this.

---
title: "Run all test-cases"
author: "STCH"
date: "`r Sys.Date()`"
output:
  html_document:
    keep_md: yes
    number_sections: yes
    toc: yes
    toc_depth: 2
    code_folding: hide
  pdf_document:
    number_sections: yes
    toc: yes
    toc_depth: '2'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10)
testfun <- function() {
    message("in testfun")
    Sys.sleep(10)
    plot(3, main = "testfun 3")
    return(0)
  }
```

# All test cases `decreasing.benefits = TRUE`

```{r}
for (i in 1){#c(1:7)) {
  cat("====================================================================\n")
  plot(1, main = "1")
  
  print("After plot 1, before plot 2")
  plot(2, main = "2")
  
  print("After plot 2, before plot 3 (Wrong!!??, appears BEFORE plot 2)")
  r <- testfun()
}
```

From the docs ?message:

message(..., domain = NULL, appendLF = TRUE)

so it seems you can only control the domain (which has to with translation) and the line ending.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source