以编程方式创建标签并在Markdown中进行绘图 [英] Programmatically create tab and plot in markdown

查看:87
本文介绍了以编程方式创建标签并在Markdown中进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的rmd中创建动态数量的标签,其中包含一些内容.
一个没有帮助.
像这样:

I'm trying to create a dynamic number of tabs in my rmd with some content inside.
This one doesn't help.
Something like 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在地图功能中生成标签.

You can set results = 'asis' knitr option to generate the tabs in the map function using cat.

Highcharterasis一起工作比较棘手:

Getting Highcharter to work with asis was trickier :

  • Highchart 需要在asis块之前被调用一次,可能需要正确初始化,因此是第一个空图表.
  • asis块形式打印图表,HTML输出以character格式发送到cat
  • Highchart needs to be called once before the asis chunck, probably to initialize properly, hence the first empty chart.
  • to print the chart in the asis chunck, the HTML output is sent in character format to 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)))
      })
)
```

请注意,尽管此解决方案运行良好,但它的使用范围已经超出了asis

Note that while this solution works well, it goes beyond the original use for asis

这篇关于以编程方式创建标签并在Markdown中进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆