0

I am trying to create a Sankey Network for energy flow from data imported from excel sheets. I don't need it to be interactive and I think I have the code right. However, when I run the code in R Markdown it just creates a blank space - no diagram. Code as follows:

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")
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56

1 Answers1

0

sankeyNetworkOutput() is used for Shiny apps, so you probably don't want to use that.

If you want to use it in RMarkdown, you probably want to add s to the last line of your code chunk, because that will tell R to "print" the s object which contians the htmlwidget that sankeyNetwork() created.

If you want to save it to a file, which it seems like based on your code having an html filename it, try using...

saveNetwork(s, "ABEnergy15.html")
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56