r - 以编程方式在 Markdown 中创建选项卡和绘图

标签 r r-markdown knitr purrr r-highcharter

我正在尝试在 rmd 中创建动态数量的选项卡里面有一些内容。
This一个没有帮助。
像这样的东西:

---
title: "1"
output: html_document
---

```{r }
library(highcharter)
library(tidyverse)
iris %>% 
      dplyr::group_split(Species) %>% 
      purrr::map(.,~{
        # create tabset for each group 
        ..1 %>% 
          hchart("scatter", hcaes(x = Sepal.Length, y = Sepal.Width))
        })
```

最佳答案

您可以设置 results = 'asis' knitr 选项使用 cat 在 map 函数中生成选项卡.
获取 Highcharterasis 一起工作更棘手:

  • Highchart 需要调用一次 之前asis chunck,可能正确初始化,因此第一个空图表。
  • asis 中打印图表chunck,HTML 输出在 character 中发送格式为 cat

  • 尝试这个:
    ---
    title: "Test tabs"
    output: html_document
    ---
    
    `r knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, cache = F)`
    
    ```{r}
    library(highcharter)
    library(tidyverse)
    # This empty chart is necessary to initialize Highcharter in the tabs
    highchart(height = 1)
    ```
    
    
    ```{r, results = 'asis'}
    cat('## Tabs panel {.tabset}   \n')
    invisible(
      iris %>% 
          dplyr::group_split(Species) %>% 
          purrr::imap(.,~{
            # create tabset for each group 
            cat('### Tab',.y,'   \n')
            cat('\n')
            p <- hchart(.x,"scatter", hcaes(x = Sepal.Length, y = Sepal.Width))
            cat(as.character(htmltools::tagList(p)))
          })
    )
    ```
    
    enter image description here
    请注意,虽然此解决方案运行良好,但它是 beyond the original use for asis

    关于r - 以编程方式在 Markdown 中创建选项卡和绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63397427/

    相关文章:

    r - rmarkdown 中的 title.sty 错误

    R plotly : Change legend symbol

    r - 在 R 中绘制带有日期的 xlim 的问题

    r - 与 data.frame 长度不等的字符向量列表

    r - 如何以程序方式将绘图嵌入到 RMarkdown 的选项卡中?

    r-markdown - 我无法编织我的 rmarkdown 文件 - pandoc 错误

    r - 从大量杂乱的数据中提取值

    r - kableExtra 包中的 group_rows() 函数不对行进行分组

    r - 如何 Markdown /html 包含加号/减号的表格

    r - 使用 markdown 中的 knitr 后的图表细微变化