Printing a sankey diagram in Shiny
Asked Answered
A

1

8

I created a sankey diagram like this:

#install.packages("networkD3")
library(networkD3)
nodes = data.frame("name" = 
                     c("Retour", # Node 0
                       "niet tevreden/ontevreden", # Node 1
                       "fout", # Node 2
                       "stuk",
                       "adres",
                       "verpakking",
                       "gebroken/glas"))# Node 3
links = as.data.frame(matrix(c(
  0, 1, 10, # Each row represents a link. The first number
  0, 2, 20, # represents the node being conntected from. 
  0, 3, 30,
  2, 4, 8,
  3, 5, 10,
  3, 6, 12# the second number represents the node connected to.
  ),# The third number is the value of the node
  byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "name",
              fontSize= 12, nodeWidth = 30)

Working fine, however now I would like to use this plot into shiny. Therefore the did the following:

## app.R ##
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(plotOutput("plot1", height = 250))
    )
  )
)

library(networkD3)
server <- function(input, output) {

  histdata <- rnorm(500)

  nodes = data.frame("name" = 
                       c("Retour", # Node 0
                         "niet tevreden/ontevreden", # Node 1
                         "fout", # Node 2
                         "stuk",
                         "adres",
                         "verpakking",
                         "gebroken/glas"))# Node 3
  links = as.data.frame(matrix(c(
    0, 1, 10, # Each row represents a link. The first number
    0, 2, 20, # represents the node being conntected from. 
    0, 3, 30,
    2, 4, 8,
    3, 5, 10,
    3, 6, 12# the second number represents the node connected to.
  ),# The third number is the value of the node
  byrow = TRUE, ncol = 3))
  names(links) = c("source", "target", "value")

  output$plot1 <- renderPlot({
    sankeyNetwork(Links = links, Nodes = nodes,
                  Source = "source", Target = "target",
                  Value = "value", NodeID = "name",
                  fontSize= 12, nodeWidth = 30)
  })
  output$plot2 <- renderPlot({
    data <- histdata[seq_len(10)]
    hist(data)
  })
}

shinyApp(ui, server)

Thing is that Shiny for some reason can read the sankey diagram. If I change the code:

box(plotOutput("plot1", height = 250))

To:

box(plotOutput("plot2", height = 250))

It is plotting the histogram. So there seems to be something wrong with the sankey diagram. Any thoughts on what is causing this?

Armyn answered 29/12, 2017 at 14:23 Comment(0)
C
11

You should use the renderSankeyNetwork and sankeyNetworkOutput functions in networkD3 instead of plotOutput and renderPlot. So with your data already loaded, it would look like...

library(shiny)
library(shinydashboard)
library(networkD3)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      sankeyNetworkOutput("plot")
    )
  )
)

server <- function(input, output) {

  output$plot <- renderSankeyNetwork({
    sankeyNetwork(Links = links, Nodes = nodes,
                  Source = "source", Target = "target",
                  Value = "value", NodeID = "name",
                  fontSize= 12, nodeWidth = 30)
  })
}

shinyApp(ui, server)

Also see the example here

Cockneyism answered 29/12, 2017 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.