具有多个绘图的动态选项卡集r标记

具有多个绘图的动态选项卡集r标记,r,r-markdown,knitr,R,R Markdown,Knitr,我设法创建了一个html文档,该文档基于项目列表创建动态选项卡集。在一个选项卡集上添加一个绘图效果很好。现在如何在一个选项卡集上添加多个绘图 我从这里开始编写代码,但当您将文档编织到html输出时,每个选项卡集只显示1个绘图。很明显,我们还缺少一些东西 --- title: "R Notebook" output: html_document: df_print: paged editor_options: chunk_output_type: inline --- ###

我设法创建了一个html文档,该文档基于项目列表创建动态选项卡集。在一个选项卡集上添加一个绘图效果很好。现在如何在一个选项卡集上添加多个绘图

我从这里开始编写代码,但当您将文档编织到html输出时,每个选项卡集只显示1个绘图。很明显,我们还缺少一些东西

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: inline
---


### header 1
```{r}
library(ggplot2)
df <- mtcars

pl_list <- list()

pl1 <- qplot(cyl, disp, data = df[1:12,])
pl2 <- qplot(mpg, cyl, data = df[13:20,])
pl3 <- qplot(mpg, cyl, data = df[21:30,])
pl4 <- qplot(mpg, cyl, data = df[1:12,])

pl_list[[1]] <- list(pl1, pl3,  "one")
pl_list[[2]] <- list(pl2, pl4,  "two")
```


### header {.tabset}
```{r, results = 'asis', echo = FALSE}

for (i in seq_along(pl_list)){
  tmp <- pl_list[[i]]
  cat("####", tmp[[3]], " \n")
  print(tmp[1])
  cat(" \n\n")
  }
```
---
标题:“R笔记本”
输出:
html_文件:
df_打印:第页
编辑器选项:
区块\输出\类型:内联
---
###标题1
```{r}
图书馆(GG2)

df您可以做一些改进

  • 创建带有文本和级别参数的
    cat
    标题函数
  • 使用它,您无需多次调用
    cat
    ,它会自动创建所需的
    号码

    catHeader <- function(text = "", level = 3) {
        cat(paste0("\n\n", 
                   paste(rep("#", level), collapse = ""), 
                   " ", text, "\n"))
    }
    

    您好,非常感谢您的时间和回复。它工作得很好,解决了我的问题!非常感谢@JLB很乐意帮忙!
    ---
    title: "R Notebook"
    output:
      html_document:
        df_print: paged
    editor_options:
      chunk_output_type: inline
    ---
    
    
    ```{r, functions}
    catHeader <- function(text = "", level = 3) {
        cat(paste0("\n\n", 
                   paste(rep("#", level), collapse = ""), 
                   " ", text, "\n"))
    }
    ```
    
    ### header 1
    
    ```{r}
    library(ggplot2)
    df <- mtcars
    
    pl_list <- list()
    
    pl1 <- qplot(cyl, disp, data = df[1:12,])
    pl2 <- qplot(mpg, cyl, data = df[13:20,])
    pl3 <- qplot(mpg, cyl, data = df[21:30,])
    pl4 <- qplot(mpg, cyl, data = df[1:12,])
    
    pl_list[[1]] <- list(pl1, pl3,  "one")
    pl_list[[2]] <- list(pl2, pl4,  "two")
    ```
    
    
    ## header {.tabset}
    
    ```{r, results = "asis", echo = FALSE}
    
    for(i in seq_along(pl_list)){
        tmp <- pl_list[[i]]
        # As you want to use tabset level here has to be lower than 
        # parent level (ie, parent is 2, so here you have to use 3)
        catHeader(tmp[[3]], 3)
        lapply(tmp[1:2], print)
    }
    ```