I have made a really nice but large sankey-diagram using the networkd3 package in r. There are 173 nodes with 4 levels of (898) links. it renders well in rstudio viewer. However, when I render it in flexdashboard I only get a compressed diagram with very thin links without the nodes. My code example is below:
library(networkD3)
library(plyr)
library(tidyverse)
#the link data
sdg_san3 <- TY %>% group_by(Target_Conditions, SDI) %>% add_count(SDI) %>% mutate(value = n * 100)
colnames(sdg_san3) <- c("source", "target", "n", "value")
#the node data
NOD_sdg3 <- data.frame(name = c(as.character(sdg_san3$source), as.character(sdg_san3$target)) %>% unique())
#making the Sankey
sdg_san3$sdsource <- match(sdg_san3$source, NOD_sdg3$name)-1
sdg_san3$sdtarget <- match(sdg_san3$target, NOD_sdg3$name)-1
s3 <- networkD3::sankeyNetwork(Links = sdg_san3, Nodes = NOD_sdg3,
Source = 'sdsource', Target = 'sdtarget',
Value = 'value', NodeID = 'name', units = 'frequency', fontSize = 12, nodeWidth = 45, height = 1500, width = 1200)
s3
In the flexdashboard, I get the image below:
I revised the code with the render functions as below:
output$sankey1 <- renderSankeyNetwork({
sdg_san3 <- TY %>% group_by(Target_Conditions, SDI) %>% add_count(SDI) %>% mutate(value = n * 100)
colnames(sdg_san3) <- c("source", "target", "n", "value")
#the node data
NOD_sdg3 <- data.frame(name = c(as.character(sdg_san3$source), as.character(sdg_san3$target)) %>% unique())
#making the Sankey
sdg_san3$sdsource <- match(sdg_san3$source, NOD_sdg3$name)-1
sdg_san3$sdtarget <- match(sdg_san3$target, NOD_sdg3$name)-1
networkD3::sankeyNetwork(Links = sdg_san3, Nodes = NOD_sdg3,
Source = 'sdsource', Target = 'sdtarget',
Value = 'value', NodeID = 'name', units = 'frequency', fontSize = 12, nodeWidth = 45, height = 30000, width = 15000)
})
sankeyNetworkOutput("sankey1")
And I still get the same result like the image above only with wider spaces between where nodes should be. Any help to properly render in the dashboard will be appreciated. Thanks