【问题标题】:Printing a sankey diagram in Shiny在 Shiny 中打印 sankey 图
【发布时间】:2018-06-09 23:29:57
【问题描述】:

我创建了一个这样的桑基图:

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

工作正常,但是现在我想将此情节用于闪亮。因此,他们做了以下事情:

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

事情是 Shiny 出于某种原因可以阅读 sankey 图。如果我更改代码:

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

收件人:

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

它正在绘制直方图。所以桑基图似乎有问题。关于造成这种情况的任何想法?

【问题讨论】:

    标签: r shiny sankey-diagram networkd3


    【解决方案1】:

    您应该在networkD3 中使用renderSankeyNetworksankeyNetworkOutput 函数,而不是plotOutputrenderPlot。因此,在您的数据已经加载的情况下,它看起来像......

    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)
    

    另见example here

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      相关资源
      最近更新 更多