R/utils.R

Defines functions try_with_time_limit render_bn

try_with_time_limit <- function(expr, cpu = Inf, elapsed = Inf) {
  y <- try({setTimeLimit(cpu, elapsed); expr}, silent = TRUE)
  if(inherits(y, "try-error")) NULL else y
}

render_bn <- function(bn) {
  nodes <- DiagrammeR::create_nodes(nodes = names(bn))
  from <- lapply(bn, function(x) x$parents)
  to <- lapply(names(bn), function(x) rep(x, each = length(from[[x]])))
  edges <- DiagrammeR::create_edges(from = unlist(from), to = unlist(to))
  gr <- DiagrammeR::create_graph(nodes_df = nodes, edges_df = edges)
  DiagrammeR::grViz(gr$dot_code)
}
jtrecenti/ea2 documentation built on May 20, 2019, 3:17 a.m.