创建一个R Shiny应用程序来显示Sankey从用户那里获取节点和链接的输入

r6l8ljro  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(93)

我正在尝试创建一个R Shiny应用程序,该应用程序将用户在链接和节点上的输入作为.csv文件,并基于该输入生成Sankey。输入文件按预期上传和显示,但Sankey未生成。
如果我一般地运行代码,没有Shiny,Sankey从我的源csv文件中生成时没有任何问题。

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)
bvhaajcl

bvhaajcl1#

(没有数据我无法测试这个,只是一种直觉,因为links()是一个被动的。

output$plot <- {
    renderSankeyNetwork({
    sankeyNetwork(Links=links(), Nodes=nodes(), Source=links()$Source, Target=links()$Target, 
                  Value = links()$Value, NodeID = links()$Name, sinksRight = FALSE, 
                  fontSize = 16, nodeWidth = 45, nodePadding = 15, 
                  margin = list(left = 175, top = 100, right = 175, bottom = 100))           
})

相关问题