【问题标题】:Programmatically create tab and plot in markdown以编程方式在 Markdown 中创建选项卡和绘图
【发布时间】:2020-12-03 10:28:06
【问题描述】:

我正在尝试在我的 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))
        })
```

【问题讨论】:

  • 那是highcharter::hchart 吗?请注意非基础包(dplyr 也可以根据purrr 安全推断?)。
  • @r2evans 是的。对此感到抱歉

标签: r r-markdown knitr purrr r-highcharter


【解决方案1】:

您可以设置results = 'asis' knitr 选项以使用cat 生成地图功能中的选项卡。

Highcharterasis 一起工作比较棘手:

  • Highchart 需要在 asis 块之前调用一次,可能是为了正确初始化,因此是第一个空图表。
  • 打印asis块中的图表,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)))
      })
)
```

请注意,虽然此解决方案运行良好,但它适用于 beyond the original use for asis

【讨论】:

  • 非常感谢! highchart(height = 1)tagList 是很好的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2019-04-30
  • 1970-01-01
相关资源
最近更新 更多