如何在我的 R shiny 应用程序中显示使用 rcharts 创建的图表?

How do I display a diagram created with rcharts within my R shiny app?

情况:

我有一个基于 rcharts 的桑基图,我想在我闪亮的应用程序中显示它(见下面的代码)。

问题:

当我调用我的 shiny 应用程序时,sankey 图并未显示在 shiny 应用程序本身中,而是显示在浏览器的另一个选项卡 window 中。该选项卡的地址是 file:///C:/Users/Me/AppData/Local/Temp/RtmpcxIBRn/rCharts159410f110f8/index.html

问题:

如何更改我的代码,以便在闪亮的应用程序本身而不是在不同的浏览器选项卡中打开桑基图?

代码:

library(shiny)
library(ggplot2)
library(networkD3)
library(rCharts)
library(igraph)

 shinyApp(

    ui = fluidPage(
     fluidRow(
        column(12,
         "Look at this Sankey-Plot:"
        )
      ),
      fluidRow(
        column(12,
         plotOutput('SankeyPl')
        )
      )
    ),

   server = function(input, output) {

  g <- graph.tree(40, children = 4)

  E(g)$weight = 1
  edgelist <- get.data.frame(g) #this will give us a data frame with from,to,weight
  colnames(edgelist) <- c("source","target","value")
  #make character rather than numeric for proper functioning
  edgelist$source <- as.character(edgelist$source)
  edgelist$target <- as.character(edgelist$target)
  sankeyPlot <- rCharts$new()
  sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
  sankeyPlot$setTemplate(script = "http://timelyportfolio.github.io/rCharts_d3_sankey/layouts/chart.html")
  sankeyPlot$set(
    data = edgelist,
    nodeWidth = 15,
    nodePadding = 10,
    layout = 32,
    width = 960,
    height = 500
  )

  output$SankeyPl=renderPlot({sankeyPlot})

    }
  )


shinyApp(ui, server)

您需要下载 these files 并将目录 d3_sankey 放在与您的应用程序相同的目录中,这样 showOutput('SankeyPl', 'd3_sankey') 中的第二个参数是指 d3_sankey 目录。例如

$ git clone https://github.com/timelyportfolio/rCharts_d3_sankey.git
$ mv rCharts_d3_sankey/libraries/widgets/d3_sankey/ <to where your shiny app is>

您的应用程序目录现在应该如下所示:

appdir/
├── app.R
└── d3_sankey

完成后,以下代码应该可以工作:

library(shiny)
library(ggplot2)
library(networkD3)
library(rCharts)
library(igraph)

shinyApp(

  ui <- fluidPage(
    fluidRow(
      column(12,
             "Look at this Sankey-Plot:"
      )
    ),
    fluidRow(
      column(12,
             showOutput('SankeyPl', 'd3_sankey')
      )
    )
  ),

  server <- function(input, output) {

    g <- graph.tree(40, children = 4)

    E(g)$weight = 1
    edgelist <- get.data.frame(g) #this will give us a data frame with from,to,weight
    colnames(edgelist) <- c("source","target","value")
    #make character rather than numeric for proper functioning
    edgelist$source <- as.character(edgelist$source)
    edgelist$target <- as.character(edgelist$target)
    sankeyPlot <- rCharts$new()
    # sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
    sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey')
    # sankeyPlot$setTemplate(script = "http://timelyportfolio.github.io/rCharts_d3_sankey/layouts/chart.html")
    sankeyPlot$set(
      data = edgelist,
      nodeWidth = 15,
      nodePadding = 10,
      layout = 32,
      width = 960,
      height = 500
    )

    output$SankeyPl <- renderChart2({
      sankeyPlot
    })

  }
)

两个主要变化是在 ui 上使用 showOutput 和在 server 上使用 renderChart2

在这里进一步阅读:

https://github.com/ramnathv/rCharts/issues/515

sankey diagram with rCharts into shiny application. Color issue