rbayesian-networks

Export bayesian network as an interactive html file


I am using bnviewer package to visualize bayesian network generated from bnlearn package in R. If I generate normal network with default colors for the nodes using the viewer function (bnlearn), I am able to export it as an html file which is interactive but if I specify different colors for different nodes based on some groupings, the figure opens in a separate window which has only 2 export options - "Generate vector file" & "Preview vector file" and I am not able to export it as a html file. Even clicking on "Open in browser" button and then saving as web page doesn't help in saving the network as an interactive html file.

Kindly help me with saving such a network with different colored nodes as an interactive html file.

Below is the sample code used to generate the visualization in bnviewer



library(bnlearn)
library(bnviewer)

data("alarm")
bn.learn.hc = hc(alarm)

clusters.legend.title = "Legend"

viewer(bn.learn.hc,
       bayesianNetwork.width = "100%",
       bayesianNetwork.height = "100vh",
       bayesianNetwork.enabled.interactive.mode = TRUE,
       bayesianNetwork.layout = "layout_on_grid",
       edges.smooth = FALSE,
       node.colors = list(background = "white",
                          border = "black",
                          highlight = list(background = "#e91eba",
                                           border = "black")),

       node.font = list(color = "black", face="Arial"),

       clusters.legend.title = list(text = clusters.legend.title,
                                    style = "font-size:18px;
                                             font-family:Arial;
                                             color:black;
                                             text-align:center;"),

       clusters.legend.options = list(

               list(label = "Pressure",
                    shape = "icon",
                    icon = list(code = "f111", size = 50, color = "#e91e63")),
               list(label = "Volume",
                    shape = "icon",
                    icon = list(code = "f111", size = 50, color = "#03a9f4")),
               list(label = "Ventilation",
                    shape = "icon",
                    icon = list(code = "f111", size = 50, color = "#4caf50")),
               list(label = "Saturation",
                    shape = "icon",
                    icon = list(code = "f111", size = 50, color = "#ffc107"))
       ),

       clusters = list(
               list(label = "Pressure",
                    shape = "icon",
                    icon = list(code = "f111", color = "#e91e63"),
                    nodes = list("CVP","BP","HRBP","PAP","PRSS")),
               list(label = "Volume",
                    shape = "icon",
                    icon = list(code = "f111", color = "#03a9f4"),
                    nodes = list("MINV","MVS","LVV","STKV")),
               list(label = "Ventilation",
                    shape = "icon",
                    icon = list(code = "f111", color = "#4caf50"),
                    nodes = list("VALV","VLNG","VTUB","VMCH")),
               list(label = "Saturation",
                    shape = "icon",
                    icon = list(code = "f111", color = "#ffc107"),
                    nodes = list("HRSA","SAO2","PVS"))
       )
)


Solution

  • This was an interesting one. If you look at the source for this package you will see a line (386) which says essentially:

    if (bayesianNetwork.enabled.interactive.mode == TRUE) {
        # Some stuff happens here to create a Shiny app
    else {
        # Some stuff happens here to create an HTML widget
    }
    

    So basically - if you want it to be interactive, you are creating a Shiny app, and you cannot then save it as an html file.

    If you change your line bayesianNetwork.enabled.interactive.mode = TRUE to FALSE then you will be able to save the object that is returned using htmlwidgets::saveWidget(). It will still be "interactive" in the sense of being able to drag the nodes around, but you will lose the additional functionality such as being able to choose the shape of the surface for the layout.