I am trying to create a R Shiny App that takes input from users on the Links and Nodes as .csv files and generates a Sankey based on that input. The input files are uploading and displaying as expected but the Sankey is not generating.
If I run the code generically, without Shiny, the Sankey is generating without any issues from my source csv files.
library(shiny)
library(DT)
library(readxl)
library(highcharter)
library(dplyr)
library(data.table)
library(networkD3)
ui = fluidPage(
titlePanel("Sankey"),
fileInput("link", "Select links file"
),
fileInput("node", "Select nodes file"
),
DT::dataTableOutput("VLout"),
DT::dataTableOutput("VNout"),
sankeyNetworkOutput("plot")
)
server <- function(input, output) {
links <- reactive({
if (is.null(input$link)) {
return("")
}
# actually read the file
data.frame(Source = read.csv(input$link$datapath)$Source,
Target = read.csv(input$link$datapath)$Target,
Value = read.csv(input$link$datapath)$Value)
})
nodes <- reactive({
if (is.null(input$node)) {
return("")
}
# actually read the file
data.frame(Name = read.csv(input$node$datapath)$Name)
})
output$VLout <- DT::renderDataTable({
links()
})
output$VNout <- DT::renderDataTable({
nodes()
})
output$plot <- {
renderSankeyNetwork({
sankeyNetwork(Links=links(), Nodes=nodes(), Source="Source", Target="Target",
Value = "Value", NodeID = "Name", sinksRight = FALSE,
fontSize = 16, nodeWidth = 45, nodePadding = 15,
margin = list(left = 175, top = 100, right = 175, bottom = 100))
})
}
}
shinyApp(ui = ui, server = server)