-1

I can't display the Sankey Graph on a shiny application through the data uploaded as a CSV using sankeyNetwork() from . Well, what I wanted to do is to enter a table as a squared matrix with all nodes and it's cases contain the weights! Simply I couldn't be able to generate Sankey graph this way described in the first part here : enter link description here The goal is to facilitate to users of the app the add of data, though they won't be obliged to enter it as "source", "target" and " weight" it only generates a link if the weight between the two nodes associated with the matrix case regroups a weight other than zero! the Link I gave presents the command of adjacency matrix which works great on console but couldn't turn it into a shiny app

server.R

library(shiny)
require(networkD3)
require(igraph)

shinyServer(function(input, output) {

  data <- reactive({
    file1 <- input$myData
    if (is.null(file1)) {
      return()
    }
    read.csv(file = file1$datapath,
             sep = input$sep,
             header = FALSE)
  })

  label <- reactive({
    file1 <- input$myLabels
    if (is.null(file1)) {
      return()
    }
    read.csv(file = file1$datapath,
             sep = input$sep,
             header = FALSE)
  })

  matrix <- function(data) {
    m = as.matrix(data)
    n = nrow(m) - 1
    colnames(m) <- c(0:n)
    return(m)
  }

  Nodes <- function(label) {
    p = as.data.frame(label$Label)
    colnames(p) <- as.factor(colnames(p))
    return(p)
  }

  Links1 <- function(matrix) {
    p = graph_from_adjacency_matrix(matrix,
                                    mode = "directed",
                                    weighted = T,
                                    diag = T)
    L = get.data.frame(p)
    return(L)
  }

  Links1$from <- function(Links1) {
    p = Links1$from
    return(p)
  }

  Links1$to <- function(Links1) {
    j = Links1$to
    return(j)
  }

  Links1$weight <- function(Links1) {
    o = Links1$weight
    return(o)
  }

  output$plot <- renderSankeyNetwork({
    sankeyNetwork(
      Links = Links1,
      Nodes = Nodes,
      Source = ' Links1$from',
      Target = 'Links1$to',
      Value = 'Links1$weight',
      NodeID = "label$Label",
      fontSize = 30,
      nodeWidth = 30
    )
  })

  output$filedf <- renderTable({
    if (is.null(data())) {
      return ()
    }
    input$file
  })

  output$sum <- renderTable({
    if (is.null(data())) {
      return ()
    }
    summary(data())
  })

  output$table <- renderTable({
    if (is.null(data())) {
      return ()
    }
    data()
  })

  output$tb <- renderUI({
    if (is.null(data()))
      h5("Powered by",
         tags$img(
           src = 'RStudio-Ball.png',
           heigth = 200,
           width = 200
         ))
    else
      tabsetPanel(
        tabPanel("About file", tableOutput("filedf")),
        tabPanel("Data",
                 tableOutput("table")),
        tabPanel("Summary", tableOutput("sum"))
      )
  })
})

ui.R

require(networkD3)
library(shiny)
require(igraph)

shinyUI(fluidPage(
  titlePanel("File Input"),
  sidebarLayout(
    sidebarPanel(
      fileInput("myData", "Upload your data"),
      fileInput("myLabels", "Upload its label as ID/Label/Nodes"),
      helpText("Default max. file size is 5MB"),
      radioButtons(
        inputId = 'sep',
        label = 'Separator',
        choices = c(
          Comma = ',',
          Semicolon = ';',
          Tab = '\t',
          Space = ''
        ),
        selected = ';'
      )
    ),
    mainPanel(sankeyNetworkOutput("plot"), uiOutput("tb"))
  )
))
Ali Fradi
  • 110
  • 1
  • 10

1 Answers1

0

Not sure what you're trying to do with all of those improperly and ultimately unused functions that you declare, but maybe this minimized example will help you get started...

(all in one R file, doesn't matter what you name the file)

library(shiny)

ui <- fluidPage(titlePanel("File Input"),
                sidebarLayout(
                  sidebarPanel(
                    fileInput("myData", "Upload your data"),
                    fileInput("myLabels", "Upload its label as ID/Label/Nodes"),
                    helpText("Default max. file size is 5MB"),
                    radioButtons(
                      inputId = 'sep',
                      label = 'Separator',
                      choices = c(
                        Comma = ',',
                        Semicolon = ';',
                        Tab = '\t',
                        Space = ' '
                      ),
                      selected = ';'
                    )
                  ),
                  mainPanel(sankeyNetworkOutput("plot"), uiOutput("tb"))
                ))

server <- function(input, output) {
  data <- reactive({
    file1 <- input$myData
    if (is.null(file1)) {
      return(NULL)
    }
    read.csv(file = file1$datapath,
             sep = input$sep,
             header = TRUE)
  })

  label <- reactive({
    file1 <- input$myLabels
    if (is.null(file1)) {
      return(NULL)
    }
    read.csv(file = file1$datapath,
             sep = input$sep,
             header = TRUE)
  })

  output$plot <- renderSankeyNetwork({
    print(names(data()))
    sankeyNetwork(
      Links = data(),
      Nodes = label(),
      Source = 'source',
      Target = 'target',
      Value = 'value',
      NodeID = "name",
      fontSize = 30,
      nodeWidth = 30
    )
  })
}

shinyApp(ui = ui, server = server)

and using the following CSV files as examples...

myData.csv

source;target;value
0;1;124.729
1;2;0.597
1;3;26.862
1;4;280.322
1;5;81.144
6;2;35
7;4;35
8;9;11.606
10;9;63.965
9;4;75.571
11;12;10.639
11;13;22.505
11;14;46.184
15;16;104.453
15;14;113.726
15;17;27.14
15;12;342.165
15;18;37.797
15;19;4.412
15;13;40.858
15;3;56.691
15;20;7.863
15;21;90.008
15;22;93.494
23;24;40.719
25;24;82.233
5;13;0.129
5;3;1.401
5;26;151.891
5;19;2.096
5;12;48.58
27;15;7.013
17;28;20.897
17;3;6.242
28;18;20.897
29;15;6.995
2;12;121.066
2;30;128.69
2;18;135.835
2;31;14.458
2;32;206.267
2;19;3.64
2;33;33.218
2;20;4.413
34;1;4.375
24;5;122.952
35;26;839.978
36;37;504.287
38;37;107.703
37;2;611.99
39;4;56.587
39;1;77.81
40;14;193.026
40;13;70.672
41;15;59.901
42;14;19.263
43;42;19.263
43;41;59.901
4;19;0.882
4;26;400.12
4;12;46.477
26;15;525.531
26;3;787.129
26;11;79.329
44;15;9.452
45;1;182.01
46;15;19.013
47;15;289.366

myLabels.csv

name
Agricultural 'waste'
Bio-conversion
Liquid
Losses
Solid
Gas
Biofuel imports
Biomass imports
Coal imports
Coal
Coal reserves
District heating
Industry
Heating and cooling - commercial
Heating and cooling - homes
Electricity grid
Over generation / exports
H2 conversion
Road transport
Agriculture
Rail transport
Lighting & appliances - commercial
Lighting & appliances - homes
Gas imports
Ngas
Gas reserves
Thermal generation
Geothermal
H2
Hydro
International shipping
Domestic aviation
International aviation
National navigation
Marine algae
Nuclear
Oil imports
Oil
Oil reserves
Other waste
Pumped heat
Solar PV
Solar Thermal
Solar
Tidal
UK land based bioenergy
Wave
Wind

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56