我正试图从excel表中导入数据,为能量流创建一个Sankey网络。我不需要它是交互式的,我认为我有正确的代码。然而,当我在R Markdown中运行代码时,它只会创建一个空格——没有关系图。代码如下:
library(dplyr)
library(shiny)
library(htmlwidgets)
library(networkD3)
nodes = data.frame("name" = c(Energy$`Alberta Energy Flow in 2015: PJ`[2:11], Energy$...5[1], Energy$...6[1], Energy$...7[1], Energy$...8[1], Energy$...9[1], Energy$...10[1], Energy$...11[1],Energy$...2[13],Energy$...3[13] ), stringsAsFactors = FALSE)
links = as.data.frame(matrix(c(
0, 10, Energy$...5[2],
1, 10, Energy$...5[3],
2, 10, Energy$...5[4],
3, 10, Energy$...5[5],
4, 10, Energy$...5[6],
5, 10, Energy$...5[7],
6, 10, Energy$...5[8],
7, 10, Energy$...5[9],
8, 10, Energy$...5[10],
9, 10, Energy$...5[11],
0, 12, Energy$...7[2],
1, 12, Energy$...7[3],
#code continues as such for a while
byrow = TRUE, ncol = 3 ))
names(links)=c("source", "target", "value")
s <- sankeyNetwork(Links=links, Nodes=nodes, Source="source", Target="target",Value="value", NodeID="name", fontSize=12, nodeWidth=25 )
sankeyNetworkOutput("ABEnergy15.html", width = "500px", height = "1000px")
sankeyNetworkOutput()
用于Shiny应用程序,所以您可能不想使用它。
如果您想在RMarkdown中使用它,您可能想将s
添加到代码块的最后一行,因为这将告诉R"打印"s
对象,该对象包含sankeyNetwork()
创建的htmlwidget。
如果你想把它保存到一个文件中,这似乎是基于你的代码有一个html文件名,请尝试使用。。。
saveNetwork(s, "ABEnergy15.html")