【问题标题】:Multiple set of global options for multiple group (type) of Rmarkdown chunk多组(类型)的 Rmarkdown 块的多组全局选项
【发布时间】:2018-01-22 00:31:37
【问题描述】:

我的文档中有很多块,其中一些并排生成 2 个图,一些并排生成 3 个图,而另一些仅用于显示结果(其中一些甚至回显代码)。我可以为文档设置全局 knitr 选项,如下所示;

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

但我想设置多组这样的全局选项,以便一些以并排的数字块为目标,一些以结果输出为目标。

由于选项只是一个列表,我可以创建多个不同选项的列表,但我想知道如何将这些选项包含在这些单独的块中?

【问题讨论】:

    标签: r knitr r-markdown options


    【解决方案1】:

    我建议使用option hooks 来完成这项工作。每当设置了某个块选项(即不是NULL)时,相应的选项挂钩就会运行并设置所需的块选项。

    以下示例定义了两组规则:“chatty”在输出前打印提示和非常有趣符号,而“silent”抑制提示并且在输出前不打印任何内容。

    同样,您可以根据需要设置fig.widthout.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")
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 2019-02-23
      • 1970-01-01
      • 2020-03-02
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      相关资源
      最近更新 更多