rhtmlwidgets

How to save collapsible treegram generated by collapsibleTree to html file in R


collapsibleTree is an awesome packgage to generate collapsible treegram in R. Does anyone know how we can save the treegram generated as html file?

org <- data.frame(
    Manager = c(
        NA, "Ana", "Ana", "Bill", "Bill", "Bill", "Claudette", "Claudette", "Danny",
        "Fred", "Fred", "Grace", "Larry", "Larry", "Nicholas", "Nicholas"
    ),
    Employee = c(
        "Ana", "Bill", "Larry", "Claudette", "Danny", "Erika", "Fred", "Grace",
        "Henri", "Ida", "Joaquin", "Kate", "Mindy", "Nicholas", "Odette", "Peter"
    ),
    Title = c(
        "President", "VP Operations", "VP Finance", "Director", "Director", "Scientist",
        "Manager", "Manager", "Jr Scientist", "Operator", "Operator", "Associate",
        "Analyst", "Director", "Accountant", "Accountant"
    )
)
library(collapsibleTree)
collapsibleTree(org, c("Manager", "Employee"), collapsed = FALSE)

Solution

  • You can use either htmltools::save_html or htmlwidgets::saveWidget.

    c = collapsibleTree(org, c("Manager", "Employee"), collapsed = FALSE)
    htmltools::save_html(c, file='file.html')
    htmlwidgets::saveWidget(c, file="file2.html")
    

    Note that the latter one will complain about not having a title.