AdeelK93 / collapsibleTree

Create Interactive Collapsible Tree Diagrams in R using D3.js
https://adeelk93.github.io/collapsibleTree/
158 stars 41 forks source link

Export to static plot? #17

Closed caleblareau closed 7 years ago

caleblareau commented 7 years ago

I've looked around a bit online for how one might do this for htmlwidgets in general, but I haven't had any luck. Is there a straightforward way to export the current plot to a static image (ideally a PDF or SVG) ? I'm using collapsibleTree in a Shiny app where users may heavily customize a tree (colors, nodes appearing, etc.) but then I want to afford them the ability to export the image to a static format.

Thanks!

AdeelK93 commented 7 years ago

I have found the webshot package to be helpful for this sort of thing, which can handle PDF exports. Especially for those sorts of exports, you'll want to use the the collapsed option that I added on the github version recently (I'll submit it to CRAN in the next week or so) so that your charts are exported with the tree fully expanded.

I attached in a sample workflow I use sometimes, though I agree that it should be more straightforward for htmlwidgets in general

library(collapsibleTree)
library(webshot)

widgetToPng <- function(widget, file = "widget.png",  ...) {
  temp <- tempfile(fileext = ".html")
  file <- R.utils::getAbsolutePath(file)
  htmlwidgets::saveWidget(widget, temp)
  webshot(
    temp, file,
    selector = "#htmlwidget_container",
    zoom = 2,
    delay = 0.5,
    ...
  )
}

widgetToPng(
  collapsibleTreeSummary(warpbreaks, c("wool", "tension", "breaks"), maxPercent = 50, collapsed = FALSE),
  "collapsibleTree.png"
)
caleblareau commented 7 years ago

Thanks! This seems really useful; I'll try implementing it tonight in my app.