Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interaction between html widgets in R shiny

I am developing an R shiny application that uses several html widgets, notably networkD3, d3heatmap and chorddiag.

These widgets work fine separately. However, using them in the same page leave a blank space where they are supposed to be.

Here is a reproducible code that shows the bug. Comment plots line in the UI and you will see plots appearing and disappearing..

I thank you very much for your help!

# libraries
library(shiny)
library(d3heatmap)
library(chorddiag)
library(networkD3)

# Server
server <- function(input, output) {

  # create heatmap
  output$heatmap <- renderD3heatmap({
    d3heatmap(mtcars, scale = "column", colors = "Spectral")
  })

  # create chord diagram
  output$chord <- renderChorddiag({
    m <- matrix(c(11975,  5871, 8916, 2868,1951, 10048, 2060, 6171, 8010, 16145, 8090, 8045,1013,   990,  940, 6907),
    byrow = TRUE, nrow = 4, ncol = 4)
    haircolors <- c("black", "blonde", "brown", "red")
    dimnames(m) <- list(have = haircolors, prefer = haircolors)
    groupColors <- c("#000000", "#FFDD89", "#957244", "#F26223")
    chorddiag(m, groupColors = groupColors, groupnamePadding = 20)
  })

  # create sankey
  output$sankey <- renderSankeyNetwork({
    nodes=data.frame(ID=c("A","B","C","D","E"))
    links=data.frame(source=c(1,2,3), target=c(3,4,4), value=c(12,15,29))
    sankeyNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target", Value = "value", NodeID = "ID")
  })

}



# Ui
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel("shiny shines"),
    mainPanel(
        # Comment these lines and you will see charts appear / disappear.
        d3heatmapOutput("heatmap"),
        chorddiagOutput("chord"),
        sankeyNetworkOutput("sankey")
    )
  )
)

shinyApp(ui = ui, server = server)
like image 414
Yan Holtz Avatar asked Sep 18 '17 01:09

Yan Holtz


1 Answers

networkD3 was updated to D3v4 in version 0.3 in. Feb. 2017, which is not compatible with v3 versions of D3, which is what chorddiag and d3heatmap appear to use. htmlwidgets, which is the underlying package that drives the above packages, only uses the most recent version of a dependency, so htmlwidgets that use conflicting versions of the same library can not both work. Check here for a starting point of discussion about this problem.

You have a few possible options, though none of them are great...

  1. revert networkD3 to a version < 0.3 so that it also uses D3v3

  2. lobby for the chorddiag developers and the d3heatmap developers to upgrade to D3v4

  3. lobby for the htmlwidgets developers to come up with a robust way of dealing with conflicting JavaScript dependencies

like image 151
CJ Yetman Avatar answered Oct 14 '22 14:10

CJ Yetman