在 Rmarkdown 中生成动态 .tabset

Generating dynamic .tabset in Rmarkdown

我想创建 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)
  }
```

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

提前致谢!

堆栈一直要求我提供 post 这个问题的更多细节,所以我必须再写一些才能被允许 post。

这是我认为可以很好地替代您的图表的东西

library(ggforce)


ggplot(iris) +
  aes(color = Species,fill = Species) +
  geom_autohistogram() +
  geom_autopoint() +
  geom_autodensity() +
  facet_matrix(vars(-Species), layer.diag = 1, layer.upper = 3, 
               grid.y.diag = FALSE)

获得预期结果的两件事:

1) 插入 {r, results = "asis"} 作为块
2) 在最后一个 printcall

之后添加 cat('\n')