timelyportfolio / sunburstR

R htmlwidget for interactive sunburst plots
http://timelyportfolio.github.io/sunburstR/articles/sunburst-2-0-0.html
Other
210 stars 123 forks source link

Error when exporting HTML-plot #99

Open kapsner opened 4 years ago

kapsner commented 4 years ago

Hello @timelyportfolio,

first of all thanks for this great R package and your well documented vignette, which makes it really easy to figure out for new users how to create nice sunburst-plots (once figured out, how data needs to be structured).

My use-case was to export a self-contained HTML-version of the plot using the R package htmlwidgets.

Although the error only occurs using the htmlwidgets R package, I think the error is related to your sunburstR package due to the different behaviour of exporting HTML pages of the both functions sunburst and sund2b.

Here is a minimal reproducible example:

dat <- data.frame(
  level1 = rep(c("a", "b"), each=3),
  level2 = paste0(rep(c("a", "b"), each=3), 1:3),
  size = c(10,5,2,3,8,6),
  stringsAsFactors = FALSE
)
tree <- d3r::d3_nest(dat, value_cols = "size")

sb1 <- sunburstR::sunburst(
  tree,
  legend = FALSE,
  width = "100%",
  height = 400
)

htmlwidgets::saveWidget(
  widget = sb1,
  file = "sunburst.html",
  selfcontained = T
)

htmlwidgets::saveWidget creates an HTML-page not displaying the plot but some JS/HTML code.

Instead, using the sund2b function to create the plot and exporting it with the same htmlwidgets::saveWidget command produces the desired result:

sb2 <- sunburstR::sund2b(
  tree,
  width = "100%",
  height = 400
)

htmlwidgets::saveWidget(
  widget = sb2,
  file = "sund2b.html",
  selfcontained = T
)

So I think, the issue is somehow related to the sunburst function and how it stores the information to produce its HTML view.