I can't display the Sankey Graph on a shiny application through the data uploaded as a CSV using sankeyNetwork()
from networkd3. 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"))
)
))