Tiny plot output from sankeyNetwork (NetworkD3) in Firefox

后端 未结 1 467
谎友^
谎友^ 2020-12-17 01:43

As per object, I get a very small plot in Firefox when using sankeyNetwork() from networkd3 in shiny but not in Chrome or RStudio.

I have not included a

相关标签:
1条回答
  • 2020-12-17 02:02

    This seems to be the result of Firefox reacting to the viewbox svg property differently than other browsers. It might be worthwhile to submit this as an issue here https://github.com/christophergandrud/networkD3/issues

    In the meantime, you could work around this by resetting the viewbox attribute using some JavaScript and htmlwidgets::onRender(). Here's an example using a minimized version of your example. (Resetting the viewbox attribute may have other consequences)

    library(htmlwidgets)
    library(networkD3)
    library(magrittr)
    
    nodes = data.frame("name" = factor(as.character(1:9)),
                       "group" = as.character(c(1,2,2,3,3,4,4,4,4)))
    
    links = as.data.frame(matrix(byrow = T, ncol = 3, c(
      0, 1, 1400,
      0, 2, 18600,
      1, 3, 400,
      1, 4, 1000,
      3, 5, 100,
      3, 6, 40,
      3, 7, 20,
      3, 8, 4
    )))
    names(links) = c("source","target","value")
    
    sn <- sankeyNetwork(Links = links, Nodes = nodes, Source = "source", 
                        Target = "target", Value = "value", NodeID = "name", 
                        NodeGroup = "group", fontSize = 12, sinksRight = FALSE)
    
    htmlwidgets::onRender(sn, 'document.getElementsByTagName("svg")[0].setAttribute("viewBox", "")')
    

    UPDATE (2019.10.26)

    This is probably a safer implementation of removing the viewBox...

    htmlwidgets::onRender(sn, 'function(el) { el.getElementsByTagName("svg")[0].removeAttribute("viewBox") }')
    

    UPDATE 2020.04.02

    My currently preferred method to do this is to use htmlwidgets::onRender to target specifically the SVG contained by the passed htmlwidget, like this...

    onRender(sn, 'function(el) { el.querySelector("svg").removeAttribute("viewBox") }')
    

    That can then be done specifically to as many htmlwidgets on your page as necessary, for instance...

    onRender(sn, 'function(el) { el.querySelector("svg").removeAttribute("viewBox") }')
    
    onRender(sn2, 'function(el) { el.querySelector("svg").removeAttribute("viewBox") }')
    
    0 讨论(0)
提交回复
热议问题