0

我在 R 包 Xaringan 中制作幻灯片,我想展示一些 Sankey 图。我将它们作为 html 文件放在一个文件夹中。如何将图表加载到我的演示文稿中?

4

1 回答 1

1

这与将 html 文件读入 Rmd 文件有关。从创建和保存 Sankey 网络开始:

# this is from the help section of NetworkD3)
library(networkD3)
# Load energy projection data
URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
              'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)

sn <-sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
             Target = 'target', Value = 'value', NodeID = 'name',
             units = 'TWh', fontSize = 12, nodeWidth = 30)

saveNetwork(sn,"Sankey.html")

然后创建一个 Rmd 文件并执行hrbrmstr在他对类似问题的回答中推荐的以下操作。

---
title: "Read Sankey into Rmarkdown file"
author: "user"
output: html_document
---

Read a Sankey diagram into a Rmd file
```{r Sankey, results='asis'}
tmp <- URLencode(paste(readLines(paste(getwd(),"Sankey.html",sep = "/")), collapse="\n"))
cat('<iframe src="data:text/html;charset=utf-8,', tmp ,'" style="border: solid; seamless:seamless; width: 100%; height: 200px"></iframe>')

于 2019-11-01T15:45:50.797 回答