尝试使用 R shiny 和 networkD3 制作桑基图时,为什么会出现 "argument "output" is missing, with no default"?

Why do I get "argument "output" is missing, with no default" when trying to make a sankey plot with R shiny and networkD3?

我正在尝试通过 networkD3 示例在 R 中制作一个闪亮的应用程序。我的代码附在底部。

我从 this page 获得了代码。

当我尝试 运行 时,我收到此错误消息:

Error in shinyServer(function(input, output) { : 
  argument "output" is missing, with no default

问题:如何修改代码使其正常工作?

library(shiny)
library(networkD3)
data(MisLinks)
data(MisNodes)

shinyUI(fluidPage(

  titlePanel("Shiny networkD3 "),

  sidebarLayout(
    sidebarPanel(
      numericInput("opacity", "Opacity", 0.6, min = 0.1, max = 1, step = .1)
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Simple Network", simpleNetworkOutput("simple")),
        tabPanel("Force Network", forceNetworkOutput("force")),
        tabPanel("Force Network with Legend & Radius", forceNetworkOutput("forceRadius")),
        tabPanel("Sankey Network", 
                 checkboxInput("sinksRight", "sinksRight", value = TRUE),
                 sankeyNetworkOutput("sankey")),
        tabPanel("Reingold-Tilford Tree", radialNetworkOutput("rt"))
      )
    )
  )
))

shinyServer(function(input, output) {


  output$sankey <- renderSankeyNetwork({
    URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
    Energy <- jsonlite::fromJSON(URL)
    sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
                  Target = "target", Value = "value", NodeID = "name",
                  fontSize = 12, nodeWidth = 30, sinksRight = input$sinksRight)
  })



})

shinyApp(ui, server)

您应该将 UI 分配给 ui,并将 server 函数分配给名为 server 的变量。然后将这些作为参数传递到最后一行 shinyApp(ui,server)。希望这对您有所帮助!

工作代码:

library(shiny)
library(networkD3)
data(MisLinks)
data(MisNodes)

ui <- shinyUI(fluidPage(

  titlePanel("Shiny networkD3 "),

  sidebarLayout(
    sidebarPanel(
      numericInput("opacity", "Opacity", 0.6, min = 0.1, max = 1, step = .1)
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Simple Network", simpleNetworkOutput("simple")),
        tabPanel("Force Network", forceNetworkOutput("force")),
        tabPanel("Force Network with Legend & Radius", forceNetworkOutput("forceRadius")),
        tabPanel("Sankey Network", 
                 checkboxInput("sinksRight", "sinksRight", value = TRUE),
                 sankeyNetworkOutput("sankey")),
        tabPanel("Reingold-Tilford Tree", radialNetworkOutput("rt"))
      )
    )
  )
))

server <- function(input, output) {


  output$sankey <- renderSankeyNetwork({
    URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
    Energy <- jsonlite::fromJSON(URL)
    sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
                  Target = "target", Value = "value", NodeID = "name",
                  fontSize = 12, nodeWidth = 30, sinksRight = input$sinksRight)
  })



}

shinyApp(ui, server)