Получить файл DOT из графика при использовании DiagrammeR - PullRequest
0 голосов
/ 12 сентября 2018

Похоже, DiagrammeR изменил свои функции create_nodes и create_edges, чем это выглядит в документе.Также атрибута $dot_code больше нет.Я не могу найти замену этому.

Вот пример кода, который был у них на документе, но он не работает.Следующий пример находится в их официальном DiagrammeR web .

###
# Create a graph with both nodes and edges
# defined, and, add some default attributes
# for nodes and edges
###

library(DiagrammeR)

# Create a node data frame
nodes <-
  create_nodes(nodes = c("a", "b", "c", "d"),
               label = FALSE,
               type = "lower",
               style = "filled",
               color = "aqua",
               shape = c("circle", "circle",
                         "rectangle", "rectangle"),
               data = c(3.5, 2.6, 9.4, 2.7))

edges <-
  create_edges(from = c("a", "b", "c"),
               to = c("d", "c", "a"),
               rel = "leading_to")


graph <-
  create_graph(nodes_df = nodes,
               edges_df = edges,
               node_attrs = "fontname = Helvetica",
               edge_attrs = c("color = blue",
                              "arrowsize = 2"))

graph
#> $nodes_df
#>   nodes label  type  style color     shape data
#> 1     a       lower filled  aqua    circle  3.5
#> 2     b       lower filled  aqua    circle  2.6
#> 3     c       lower filled  aqua rectangle  9.4
#> 4     d       lower filled  aqua rectangle  2.7
#>
#> $edges_df
#>   from to        rel
#> 1    a  d leading_to
#> 2    b  c leading_to
#> 3    c  a leading_to
#>
#> $graph_attrs
#> [1] NULL
#>
#> $node_attrs
#> [1] "fontname = Helvetica"
#>
#> $edge_attrs
#> [1] "color = blue"  "arrowsize = 2"
#>
#> $directed
#> [1] TRUE
#>
#> $dot_code
#> [1] "digraph {\n\ngraph [rankdir = LR]\n\nnode [fontnam...
#>
#> attr(,"class")
#> [1] "dgr_graph"

# View the graph in the RStudio Viewer
render_graph(graph)

1 Ответ

0 голосов
/ 08 июня 2019

обратитесь за помощью к пакету DiagrammeR, веб-сайт действительно не обновлен.Посмотрите на функцию create_graph () в справке, которая показывает, как изменился синтаксис.

что-то вроде этого должно сделать то же самое:

    ###

library(DiagrammeR)

# Create a node data frame (ndf) where the labels
# are equivalent to the node ID values (this is not
# recommended); the `label` and `type` node
# attributes will always be a `character` class
# whereas `id` will always be an `integer`
nodes <-
  create_node_df(
    n         = 4,
    type      = "lower", 
    label     = c("a", "b", "c", "d"),
    style     = "filled",
    fillcolor = "blue",
    shape     = c("circle", "circle",
                  "rectangle", "rectangle"),
    data      = c(3.5, 2.6, 9.4, 2.7))

# Create an edf with additional edge
# attributes (where their classes will
# be inferred from the input vectors)
edges <-
  create_edge_df(
    from = c(1, 2, 3),
    to   = c(4, 3, 1),
    rel  = "leading_to")

# With `create_graph()` we can
# simply create an empty graph (and
# add in nodes and edges later
# with other functions)
graph <-
  create_graph(
    nodes_df  = nodes,
    edges_df  = edges)     %>%
  set_edge_attrs(
    edge_attr = color,
    values    = "green")   %>%
  set_edge_attrs(
    edge_attr = arrowsize,
    values    = 2)         %>%
  set_node_attrs(
    node_attr = fontname,
    values    = "Helvetica")

graph

render_graph(graph)
...