sankey plot in Shiny

2,624 views
Skip to first unread message

andya...@gmail.com

unread,
Jan 22, 2014, 4:53:33 PM1/22/14
to shiny-...@googlegroups.com
Hi,

I'm trying to generate rCharts sankey diagram in Shiny however I've been unsuccessful so far. I do not get any errors but the diagram does not display.  Does anyone have some experience with rCharts sankey diagram and Shiny ?


server.r
require(rCharts)
require(shiny)

   dd <-  dataframe(data)

shinyServer(function(input, output){
 
 output$sankey <-  renderChart2({
  
   
     sankeyPlot <- rCharts$new()
   sankeyPlot$set(
      data = dd,
      nodeWidth = 15,
      nodePadding = 10,
      layout = 32,
      width = 400,
      height = 200
    )
    
    return(sankeyPlot)
    }) 
})

ui.r


require(rCharts)
require(shiny)

shinyUI(pageWithSidebar(
  
  headerPanel("CTCA"), 
  
  sidebarPanel(
    
    
     selectInput("segment_name" , p(strong("Select Segment Type")),
                 c(unique(dd$name)))
  ),
    
    mainPanel( 
      showOutput("sankey", "polycharts")
      
      )
))


Andrew Clark

unread,
Jan 24, 2014, 1:29:18 PM1/24/14
to shiny-...@googlegroups.com
Does the chart render if you create one outside of shiny

It is not clear to me in your code where the input$segment_name is utilized

It would probably help if you could post a gist with some sample data

andya...@gmail.com

unread,
Jan 24, 2014, 2:19:47 PM1/24/14
to Andrew Clark, shiny-...@googlegroups.com
Yes the chart does render outside shiny . Segment_name is a column name in my data , not used for Stanley chart . 

Sent from my iPhone
--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/Ob8PjnmA4RQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Patrick Toche

unread,
Jan 25, 2014, 2:43:46 PM1/25/14
to shiny-...@googlegroups.com, Andrew Clark
does it help if you wrap it in a print() ?

andy adamiec

unread,
Jan 27, 2014, 11:54:17 PM1/27/14
to shiny-...@googlegroups.com, Andrew Clark
yes but with print new page opens in the browser and chart is generated on that page instead shiny .

Ramnath Vaidyanathan

unread,
Jan 28, 2014, 3:05:12 PM1/28/14
to shiny-...@googlegroups.com, Andrew Clark
I see two errors. 

1. You need to use this URL in setLib http://timelyportfolio.github.com/rCharts_d3_sankey
2. It should be showOutput("sankey", "sankey"). The second argument is the name of the library.

Ramnath

andy adamiec

unread,
Jan 30, 2014, 5:13:30 PM1/30/14
to shiny-...@googlegroups.com, Andrew Clark
i agree with 1, in 2 the second argument can not be sankey there is no such library

Ramnath Vaidyanathan

unread,
Jan 31, 2014, 2:56:49 AM1/31/14
to shiny-...@googlegroups.com, Andrew Clark
You are right. Here is what I suggest.


You could download this github repo, unzip its contents and rename the folder specified above as `sankey` and then refer to it in `setLib`.

Adomas Budrys

unread,
Feb 25, 2014, 4:38:14 AM2/25/14
to shiny-...@googlegroups.com, Andrew Clark
Hi, 

I am trying to generate sankey diagram too, using script produced above. I specified direct path to library, where d3_sankey folder exist. So I added:
sankeyPlot$setLib('C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/d3_sankey')
sankeyPlot$setTemplate(script = "C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/d3_sankey/layouts/chart.html")

And set showOutput("sankey", "sankey") too. 

However, after launchning app, the browser sometime opens normally, but sometimes gives following error (usually on first run):
ERROR: path[1]="": The filename, directory name, or volume label syntax is incorrect

Then I just reload browser page and normal application screen appears. The problem is that in the place, where sankey diagram should appear, only question mark (?) appears and nothing more. In the R environment error pops up:
Warning in readLines(file, warn = warn, ...) :
  invalid input found on input connection 'C:/Users/adomas/Documents/R/win-library/3.0/rCharts/libraries/widgets/d3_sankey/layouts/chart.html

Then if I try it just on R, then it works. 

Buuuut, the strange thing is that sometimes it doesn't work on just R too. Warning about 'invalid input found on input connection' appears. Then I just restart Rstudio and everything works again. Is there any options to fix these problem? 

Thanks, Adomas

Leah Soriaga

unread,
Mar 20, 2014, 3:25:09 PM3/20/14
to shiny-...@googlegroups.com
I was wondering if this issue has been resolved and if anyone has a minimal example of a functioning shiny app with a sankey diagram.

I have run into similar issues where I could replicate the code here: https://gist.github.com/timelyportfolio/6001601 and the sankey diagram displays in the plot viewer in RStudio, but when I integrate within a shiny app, nothing is plotted on the page.  When I inspect the page I find this:

<div id="Plot" class="shiny-html-output rChart sankey shiny-bound-output">
      </div>

Any help would be appreciated.

I have used combinations of:
output$my_sankey <-renderChart in ui. R with  
showOutput("my_sankey","sankey") in server.R

output$my_sankey <-renderChart2 in ui. R with
chartOutput("my_sankey","sankey") in server.R

I have copied the files from the repo as described above and renamed the folder "sankey".
I then reference the full path to the folder in the setLib argument.

ui.R code:
require(rCharts)
require(shiny)
shinyUI(
  pageWithSidebar(
    headerPanel('Test'),
    sidebarPanel(  'Test'  ),
    mainPanel(
      showOutput("Plot", 'sankey')  
    )
    )
  )
server.R code:
require(rCharts)
require(rjson)

shinyServer(function(input, output){

  
  links <- matrix(unlist(
    rjson::fromJSON(
    )$links
  ),ncol = 3, byrow = TRUE)
  nodes <- unlist(
    rjson::fromJSON(
    )$nodes
  )
  
  links <- data.frame(links)
  colnames(links) <- c("source", "target", "value")
  links$source <- sapply(links$source, FUN = function(x) {return(as.character(nodes[x+1]))}) #x+1 since js starts at 0
  links$target <- sapply(links$target, FUN = function(x) {return(nodes[x+1])}) #x+1 since js starts at 0
  
  
    output$Plot <-  renderChart2({
      sankeyPlot2 <- rCharts$new()
      sankeyPlot2$setLib('/home/XXX/R/XXX/3.0/rCharts/libraries/sankey')
      sankeyPlot2$set(
        data = links,
        nodeWidth = 15,
        nodePadding = 10,
        layout = 32,
        width = 500,
        height = 500,
        units = "TWh",
        title = "Sankey Diagram"
      )
      return(sankeyPlot2)
    })
  }
)
sessionInfo:R version 3.0.2 (2013-09-25) Platform: x86_64-redhat-linux-gnu (64-bit) locale: [1] C attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] shiny_0.8.0 devtools_1.4.1 igraph_0.7.0 rCharts_0.4.2 VennDiagram_1.6.5 reshape_0.8.4 plyr_1.8 reshape2_1.2.2 rmarkdown_0.1.6 [10] ggplot2_0.9.3.1 loaded via a namespace (and not attached): [1] MASS_7.3-29 RColorBrewer_1.0-5 RCurl_1.95-4.1 RJSONIO_1.0-3 Rcpp_0.11.0 bitops_1.0-6 caTools_1.16 colorspace_1.2-4 [9] dichromat_2.0-0 digest_0.6.4 evaluate_0.5.1 gtable_0.1.2 httpuv_1.2.2 httr_0.2 labeling_0.2 lattice_0.20-23 [17] memoise_0.1 munsell_0.4.2 parallel_3.0.2 proto_0.3-10 rjson_0.2.13 scales_0.2.3 stringr_0.6.2 tools_3.0.2 [25] whisker_0.3-2 xtable_1.7-1 yaml_2.1.10 

Thanks,
Leah

andy adamiec

unread,
Mar 20, 2014, 3:34:23 PM3/20/14
to Leah Soriaga, shiny-...@googlegroups.com
googlevis package supports sankey chart that I've used successfully
with shiny. Very easy to use, but less colorful.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Shiny - Web Framework for R" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/shiny-discuss/Ob8PjnmA4RQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> shiny-discus...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Leah Soriaga

unread,
Mar 20, 2014, 4:44:01 PM3/20/14
to andy adamiec, shiny-...@googlegroups.com
Thank you for the tip! gvisSankey worked great for me.

eunji noh

unread,
May 6, 2014, 2:27:56 AM5/6/14
to shiny-...@googlegroups.com, andy adamiec
Did anyone figured out how to integrate rChart's sankey diagram with Shiny yet?

Thanks in advance.

car...@xray-imatek.com

unread,
Sep 3, 2014, 1:01:29 PM9/3/14
to shiny-...@googlegroups.com
Hi,

After looking and mixing and matching some comments from Timelyportfolio and Ramnath, I was able to get this to work. Now, my only issue is that

sankeyPlot$setTemplate(
   afterScript = "<script> Whatever </script>"
)

doesn't seem to have any effect.

Here it is a simple example:

ui.R

library(shiny)
library(rCharts)

shinyUI(bootstrapPage(
  div(
    class="container-fluid",
    div(class="row-fluid",
       headerPanel("Header Panel")
    ),
    div(class="row-fluid",
        sidebarPanel(
          h4("Empty")
        ),
        mainPanel(
          tableOutput("table"),
           showOutput('sankey', 'd3_sankey') # I refer to this directory later on in this message
        )
    )
  )
))

server.R

library(shiny)
library(rCharts)

shinyServer(function(input, output){
  data <- reactive({
    data <- data.frame(source=c("A","A","A","B","G","H","H","L"),
               target=c("B","L","G","E","M","L","B","M"),
               value=c("1","3","2","3","2","7","2","25"))
    return(data)
  })
 
  output$table <- renderTable({
    return(data())
  })

 
  output$sankey <-  renderChart2({ 
    sankeyPlot <- rCharts$new()
    sankeyPlot$setLib("http://timelyportfolio.github.io/rCharts_d3_sankey")

    sankeyPlot$set(
      data = data(),

      nodeWidth = 15,
      nodePadding = 10,
      layout = 32,
      width = 500,
      height = 500
    )
    return(sankeyPlot)
  })
})

The final piece of the puzzle is to download the d3_sankey directory

https://github.com/timelyportfolio/rCharts_d3_sankey/tree/gh-pages/libraries/widgets/d3_sankey

and copy it to the same folder where you have put ui.R and server.R

Carlos

Herman Sontrop

unread,
Sep 3, 2014, 2:36:21 PM9/3/14
to shiny-...@googlegroups.com
Hi Carlos,

You may want to check out this recent implementation of Sankey d3:
https://github.com/christophergandrud/networkD3/tree/master:

It builds on an upcoming package HTMLWidgets from the author of rCharts in collaboration with RStudio, it also works nicely in Shiny.

Best Herman

Vaughn Shirey

unread,
Jun 22, 2016, 12:07:59 PM6/22/16
to Shiny - Web Framework for R, lea...@gmail.com
Do you have any insight/example code for this? I'm currently working on trying to integrate Gvis Sankey with Shiny but haven't had any luck - even without a reactive element.

sunny...@gmail.com

unread,
Jan 31, 2017, 12:14:09 PM1/31/17
to Shiny - Web Framework for R
Its giving me error  there is no package called ‘rCharts’. Can any share sample app code if possible
Reply all
Reply to author
Forward
0 new messages