christophergandrud / networkD3

D3 JavaScript Network Graphs from R
http://christophergandrud.github.io/networkD3
652 stars 269 forks source link

package conflict with other package in shiny #235

Closed cococ0j closed 6 years ago

cococ0j commented 6 years ago

Hi Chris, thank you for your awesome package. I am doing a project using both your networkD3 package and chorddiag package to create one visualization by shiny. However, I found that these two packages have some kind of confilct. I use sandkey diagram and chord diagram, but these two graph can not appear in one page. I 'm sure my code has no error. Could you please help me?

cjyetman commented 6 years ago

If you're talking about mattflor's chorddiag, it's because chorddiag uses D3v3 while networkD3 has been updated to use D3v4, D3v3 and D3v4 are not compatible, and htmlwidgets does not allow multiple versions of the same named dependency to be used in one page. This underlying problem has been reported multiple times, and has an overarching 'resolution' issue open here #162. networkD3 does have its own version of a chord diagram/plot, which can be used side-by-side with its sankey plot in shiny, for example...

library(shiny)
library(networkD3)

hairColourData <- matrix(c(11975,  1951,  8010, 1013,
                           5871, 10048, 16145,  990,
                           8916,  2060,  8090,  940,
                           2868,  6171,  8045, 6907),
                         nrow = 4)

URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
              'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)

server <- function(input, output) {
  output$chord <- renderchordNetwork({
    chordNetwork(Data = hairColourData, width = 500, height = 500,
                 colourScale = c("#000000", "#FFDD89", "#957244", "#F26223"),
                 labels = c("red", "brown", "blond", "gray"))
  })

  output$sankey <- renderSankeyNetwork({
    sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
                  Target = 'target', Value = 'value', NodeID = 'name',
                  units = 'TWh', fontSize = 12, nodeWidth = 30)
  })
}

ui <- shinyUI(fluidPage(
  chordNetworkOutput("chord"),
  sankeyNetworkOutput("sankey")
))

shinyApp(ui = ui, server = server)
cococ0j commented 6 years ago

@cjyetman Thank you so much, cjyetman. I am new to your package and I also tried your version of chord diagram. However, it returns an error 'Data must have the same number of rows and columns; given 24 rows and 12 columns'.

In my data, I tried to explore relationship between two categories : one has 24 candidates, the other one has 12. Does your chord diagram only work with two equal size groups? Please forgive me if this is a silly question.

cjyetman commented 6 years ago

Please don't use GitHub Issues as a support forum. This is meant for submitting bug reports and tracking feature requests, etc. This type of question is better served on StackOverflow, for instance.

Yes, currently networkD3's chordNetwork() requires a "square matrix" as stated in the help file.