2

Situation:

I have a sankey diagram based on rcharts that I want to display in my shiny app (see code below).

Problem:

When I call my shiny app, the sankey diagram is not shown within the shiny app itself but rather in a different tab window of the browser. The tab's address is file:///C:/Users/Me/AppData/Local/Temp/RtmpcxIBRn/rCharts159410f110f8/index.html

Question:

How do I change my code so that the sankey diagram is opened within the shiny app itself rather than within a different browser tab?

Code:

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)
nexonvantec
  • 572
  • 1
  • 5
  • 18
  • sankeyPlot is an rcharts graph. You might have to use something different than renderPlot(). In older posts, they use showOutput('SankeyPl', 'd3_sankey') and renderChart2({}) for rcharts plot. It might be the issue. But it does not work anymore. – MLavoie Feb 06 '18 at 10:57
  • see here: https://stackoverflow.com/questions/48288084/rcharts-shiny-plot-not-showing – MLavoie Feb 06 '18 at 11:02
  • and see: https://groups.google.com/forum/#!topic/shiny-discuss/Ob8PjnmA4RQ – MLavoie Feb 06 '18 at 11:12
  • 1
    I would suggest using the `htmlwidgets` Sankey implementation in [networkD3](https://github.com/christophergandrud/networkD3) since `rCharts` has been deprecated. – timelyportfolio Feb 07 '18 at 11:38

1 Answers1

2

You need to download these files and place the directory d3_sankey in the same directory as your app, such that the second argument in showOutput('SankeyPl', 'd3_sankey') is referring to the d3_sankey directory. E.g.

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

Your app directory should now look like this:

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

Once you have done that, the following code should work:

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

  }
)

The two major changes are the use of showOutput on the ui and renderChart2 on the server.

Further reading here:

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

sankey diagram with rCharts into shiny application. Color issue

Luke Singham
  • 1,536
  • 2
  • 20
  • 38
  • Thank you Luke. This solution is almost perfect. One little issue still: The solution produces the cryptic expression "" at the end of the plot. Do you know why? – nexonvantec Feb 06 '18 at 12:38
  • 1
    @nexonvantec If the answer fixes the 'open within shiny' issue, could the answer please be accepted? I can't replicate the "" issue - I'm on OSX and have tried both chrome and firefox. This question is a start https://stackoverflow.com/questions/37264754/strange-symbol-%C3%AF-appearing-from-rchart-sankey-output-in-shiny – Luke Singham Feb 06 '18 at 12:58