Multiple sets of global options for multiple groups (type) Rmarkdown chunk

I have many pieces in my documents, some of them produce 2 plots side by side, some of them produce 3 plots side by side, and some others only to display the results (among which some even drive off the code). I can set the global knitr specification for a document as follows:

```{r setup, include = FALSE, cache = FALSE}
knitr::opts_chunk$set(
  comment = NA,
  fig.width = 7,
  out.width = 50%,
  warning = FALSE
)
```

But I want to configure several sets of such global parameters, so that some will focus on curly pieces with a double figure side by side, and some will focus on the result.

Since parameters are just a list, I can create several lists of different parameters, but I wonder how to include these parameters in these separate pieces?

+4
1

hooks . , chunk (.. NULL), .

: "chatty" , "silent" .

, fig.width out.width .

```{r, echo = FALSE}
library(knitr)
opts_hooks$set(chatty = function(options) {
  options$prompt = TRUE
  options$comment = ";-)"
  return(options)
})

opts_hooks$set(silent = function(options) {
  options$promt = FALSE
  options$comment = ""
  return(options)
})
```

# Demo

## Default

```{r}
print("Default")
```

## Chatty

```{r, chatty = TRUE}
print("chatty")
```

## Silent

```{r, silent = TRUE}
print("silent")
```
+3

Source: https://habr.com/ru/post/1683647/


All Articles