从 html 文件中读取桑基图
Read a Sankey Diagram from a html file
我正在用 R 包 Xaringan 制作幻灯片,我想展示一些 Sankey 图。我将它们作为 html 文件放在一个文件夹中。如何将图表加载到我的演示文稿中?
这与将 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 in his answer to a 推荐的以下操作。
---
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>')
我正在用 R 包 Xaringan 制作幻灯片,我想展示一些 Sankey 图。我将它们作为 html 文件放在一个文件夹中。如何将图表加载到我的演示文稿中?
这与将 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 in his answer to a
---
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>')