You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将带分组节点颜色的贝叶斯网络导出为交互式HTML文件?

解决带分组颜色的贝叶斯网络交互式HTML导出问题

当使用bnviewer的clusters参数为节点分组设置颜色后,内置导出按钮丢失HTML选项的问题,可以通过直接保存viewer()返回的HTML部件对象来解决,具体步骤如下:

  • 先安装并加载htmlwidgets包,这是R中处理HTML交互式部件的工具包
  • 将viewer()的渲染结果赋值给变量,而非直接输出
  • 用saveWidget()函数将变量保存为独立的交互式HTML文件

修改后的完整代码:

library(bnlearn)
library(bnviewer)
library(htmlwidgets) # 加载处理HTML部件的包

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

clusters.legend.title = "Legend"

# 将viewer返回的交互式部件赋值给变量
bn_interactive <- 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"))
       )
)

# 保存为独立的交互式HTML文件
saveWidget(bn_interactive, file = "bayesian_network_clustered.html", selfcontained = TRUE)

补充说明

  • selfcontained = TRUE参数会把所有依赖的样式、脚本打包到单个HTML文件中,无需额外文件即可打开使用
  • 执行代码后,当前工作目录下会生成指定名称的HTML文件,打开后就能看到带分组颜色的交互式贝叶斯网络

内容的提问来源于stack exchange,提问作者Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 12:55:15