默认自定义选项

时间:2017-11-02 22:52:46

标签: r knitr

我想为我的文档创建一个默认的块选项,以便我可以选择附录中包含哪些块。这是我的MWE:

```{r setup, include=FALSE, appendix=FALSE}
knitr::opts_chunk$set(echo=FALSE)
knitr::opts_chunk$set(appendix=TRUE)
```

```{r}
# some code included in the appendix
```

```{r appendix=FALSE}
# some code not included in the appendix
```

# Appendix
```{r, ref.label=knitr::all_labels(appendix), echo = T, eval = F}
```

我想包含所有未明确说明appendix=TRUE的块。如果我添加它,它会按预期工作,但默认knitr::opts_chunk$set(appendix=TRUE)似乎不起作用。

我可能在设置中遗漏了一些东西。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您需要明确设置:

    Appendix <- TRUE
  • setOptions
然后你设置 -

  • knitr::opts_chunk$set(appendix = TRUE)

现在任何没有Appendix = FALSE的块都包含在附录中。

注意:在附录块中包含Appendix = FALSE非常重要。

我希望上述方法有所帮助,我就是这样做的。

---
title: "47085866-2"
author: "Technophobe1"
date: "11/5/2017"
output: 
  html_document: 
    keep_md: yes
---

```{r setOptions, echo = FALSE, Appendix = FALSE}
Appendix <- TRUE
knitr::opts_chunk$set(echo = FALSE, message = FALSE, error = FALSE, warning = FALSE, results = 'hide')
knitr::opts_chunk$set(appendix = TRUE)

```

## Appendix = TRUE

```{r code}
# some code included in the appendix

setClass(
  "CStruct",
  slots = list(
    powerLevel = "numeric",
    size = "numeric"
  )
)

CStructure <- new("CStruct", powerLevel = 5, size = 10)
CStructure
str(CStructure)
CStructure@powerLevel
CStructure@size

```

## Appendix = FALSE
```{r ref.label='code', Appendix = FALSE}
# some code not included in the appendix
setClass(
  "CStruct2",
  slots = list(
   n = "numeric",
   s = "character",
   b = "logical"
  )
)

CStructure2 <- new("CStruct2", n = c(2, 3, 5), 
                  s = c("aa", "bb"), 
                  b = c(TRUE, FALSE, TRUE) )
str(CStructure2)
CStructure2@n
CStructure2@s
CStructure2@b
```
# Code Appendix
```{r, ref.label=knitr::all_labels(Appendix), Appendix = FALSE, echo=TRUE, eval=FALSE}
## Code Appendix


```

输出:

enter image description here