首页 > 解决方案 > 在 Rmarkdown 中生成动态 .tabset

问题描述

我想创建 3 个 .tabsets,每个级别的 Iris 数据集上的 Species 一个(示例)。在每个 .tabset 上,需要 3 个或更多选项卡。每个选项卡都有你自己的情节。

我写了这段代码:

```{r echo=FALSE, results='asis'}

library(tidyverse)

for(especies in levels(iris$Species)){ 
  cat('\n#', especies, '{.tabset}', '\n')
  cat('\n##',  'Petal Lengh x Sepal Length', '\n')
  iris %>%
    filter(
      Species == especies
    ) %>%
    ggplot(
      aes(
        x = Sepal.Length,
        y = Petal.Length
      )
    ) +
    geom_point() -> p
  print(p)
  cat('\n')
  cat('\n##', 'Sepal Length', '\n')
    iris %>%
    filter(
      Species == especies
    ) %>%
    ggplot(
      aes(
        x = Sepal.Length
      )
    ) +
    geom_histogram() -> p
  print(p)
  cat('\n')
  cat('\n##', 'Petal Length', '\n')
    iris %>%
    filter(
      Species == especies
    ) %>%
    ggplot(
      aes(
        x = Petal.Length
      )
    ) +
    geom_histogram() -> p
  print(p)
  }
```

我不知道为什么它不起作用。有任何想法吗?

提前致谢!

堆栈不断要求我提供更多详细信息以发布问题,所以我必须再写一些才能被允许发布。

标签: rr-markdownknitrpandoc

解决方案


获得预期结果的两件事:

1){r, results = "asis"}作为块插入
2)cat('\n')在最后一次print调用之后添加


推荐阅读