Вот обходной путь для вывода HTML, который автоматически решает, сколько столбцов необходимо.Этот подход основан на настройке ловушки чанка для изменения вывода уценки.Мы в основном просто выполняем строковые операции (поиск и замена).Я надеюсь, что комментарии в коде достаточно чисты:
MRE:
---
title: "Test File"
output: html_document
---
## R Markdown
```{r, include = F}
library(stringi)
defChunkHook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- defChunkHook(x, options)
if(!is.null(options$multi.col)) {
x <- gsub("(\n\`\`\`\n##.*?\`\`\`)", "<div>\\1\n</div>", x) # wrap div around output chunks
x <- gsub("(<img.*?>)", "<div>\\1\n</div>", x) # wrap div around plots
ncol <- nrow(stri_locate_all(x, regex = "<div.*?>")[[1]]) # get the number of div elements created
x <- gsub("<div>", paste0("<div style=\"width:", 100/ncol,"%;\">"), x) # add the width to the divs
x <- paste0("<div class=\"multi-col\" style=\"display: flex; justify-content: center; align-items: center;\">\n", x, "</div>") # wrap the mother div around all of the output
}
x
})
```
```{r, echo = F, multi.col=T}
summary(cars)
plot(speed ~ dist, cars)
```
```{r, echo = F, multi.col=T}
summary(cars)
plot(speed ~ dist, cars)
plot(mpg ~ hp, mtcars)
```