Skip to content

Commit

Permalink
rendering graphs
Browse files Browse the repository at this point in the history
DiagrammeR interface changed
  • Loading branch information
bergant committed Jan 11, 2017
1 parent 365c575 commit ef5d0ef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 45 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Package: datamodelr
Type: Package
Title: Define and plot data model diagrams
Version: 0.1.2.9002
Date: 2016-04-12
Version: 0.2.1.9001
Date: 2017-01-12
Author: Darko Bergant
Maintainer: Darko Bergant <[email protected]>
URL: https://github.com/bergant/datamodelr
Description: Data model definition with YAML file,
extract from R data frames,
reverse-engineering from PostgreSQL and SQL Server and
rendering with DiagrammeR graph objects.
rendering with DiagrammeR/graphviz.
Depends:
R (>= 3.2)
R (>= 3.3)
License: MIT + file LICENSE
Suggests:
yaml,
Expand Down
49 changes: 23 additions & 26 deletions R/graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,21 @@ dm_create_graph_list <- function(dm, view_type = "all",
})

nodes <-
list(
data.frame(
nodes = names(tables),
label = g_labels,
shape = "plaintext",
type = "upper",
segment = dm$tables[order(dm$tables$table), "segment"]
segment = dm$tables[order(dm$tables$table), "segment"],

stringsAsFactors = FALSE
)


if(!is.null(dm$references)) {
edges <-
with(dm$references[dm$references$ref_col_num == 1,],
DiagrammeR::create_edges(from = table, to = ref))
data.frame(from = table, to = ref, stringsAsFactors = FALSE))
} else {
edges <- NULL
}
Expand Down Expand Up @@ -236,10 +238,6 @@ dm_create_graph <- function(dm, rankdir = "BT", graph_name = "Data Model",

if(!is.data_model(dm)) stop("Input must be a data model object.")

if( !requireNamespace("DiagrammeR", quietly = TRUE)) {
stop("DiagrammeR package needed for this function to work. Please install it.",
call. = FALSE)
}

if(!all(col_attr %in% names(dm$columns) )) {
stop("Not all col_attr in data model column attributes.")
Expand All @@ -251,53 +249,52 @@ dm_create_graph <- function(dm, rankdir = "BT", graph_name = "Data Model",
warning("The number of tables to render is 0.")
}
graph <-
DiagrammeR::create_graph(
list(
graph_attrs = sprintf('rankdir=%s tooltip="%s" %s', rankdir, graph_name, graph_attrs),
node_attrs = sprintf('margin=0 fontcolor = "#444444" %s', node_attrs),
nodes_df = do.call(DiagrammeR::create_nodes, g_list$nodes),
edges_df = if(!is.null(g_list$edges)) do.call(DiagrammeR::create_edges, g_list$edges) else NULL,
nodes_df = g_list$nodes,
edges_df = g_list$edges,
edge_attrs = c('color = "#555555"',"arrowsize = 1", edge_attrs)
)
class(graph) <- c("datamodelr_graph", class(graph))

# re-create dot code for data model
# (DiagrammeR does not support yet the HTML labels and clusters (v.0.8))
graph$dot_code <- dot_graph(graph)
sessionInfo()

graph

}


#' Render graph
#'
#' A wrapper around DiagrammeR::render_graph
#' Using DiagrammeR to render datamodelr graph object
#'
#' @param graph a \pkg{DiagrammeR} dgr_graph object
#' @param output string specifying the output type; graph (the default) renders
#' the graph using the grViz function,
#' vivagraph renders the graph using the vivagraph function,
#' visNetwork renders the graph using the visnetwork function,
#' DOT outputs DOT code for the graph, and SVG provides SVG code for the rendered graph.
#' @param layout a string specifying a layout type for a vivagraph rendering of the graph,
#' either forceDirected or constant.
#' @param graph a graph object
#' @param width an optional parameter for specifying the width of the resulting
#' graphic in pixels.
#' @param height an optional parameter for specifying the height of the resulting graphic in pixels.
#' @export
dm_render_graph <- function (graph, output = "graph", layout = NULL, width = NULL,
height = NULL) {
dm_render_graph <- function (graph, width = NULL, height = NULL) {

if( !requireNamespace("DiagrammeR", quietly = TRUE)) {
stop("DiagrammeR package needed for this function to work. Please install it.",
call. = FALSE)
}


if(substring(graph$dot_code, 1, 11) != "#data_model") {
if(is.null(graph$dot_code)) {
graph$dot_code <- dot_graph(graph)
}

DiagrammeR::render_graph(graph, output, layout, width, height)
DiagrammeR::grViz(graph$dot_code, allow_subst = FALSE, width, height)
}


dot_graph <- function(graph) {

graph_type <- ifelse(graph$directed, "digraph", "graph")
graph_type <- "digraph"

dot_attr <- paste0(
sprintf("graph [%s]\n\n", paste(graph$graph_attrs, collapse = ", ")),
Expand All @@ -306,7 +303,7 @@ dot_graph <- function(graph) {
)
segments <- unique(graph$nodes_df$segment)
segments <- segments[!is.na(segments)]
segments <- setNames(1:(length(segments)), segments)
segments <- stats::setNames(1:(length(segments)), segments)

dot_nodes <- sapply(seq_len(nrow(graph$nodes_df)), function(n) {
node <- graph$nodes_df[n,]
Expand Down
16 changes: 3 additions & 13 deletions man/dm_render_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ test_that("Example - simple", {
")

graph <- dm_create_graph(dm)
expect_is(graph, "dgr_graph")
expect_is(graph, "datamodelr_graph")
})

test_that("Example - graph - example.yml", {

file_path <- system.file("samples/example.yml", package = "datamodelr")
dm <- dm_read_yaml(file_path)
graph <- dm_create_graph(dm)
expect_is(graph, "dgr_graph")
expect_is(graph, "datamodelr_graph")
})

0 comments on commit ef5d0ef

Please sign in to comment.