get_attr_dfs {DiagrammeR} | R Documentation

## Get data frames bound to node attributes

### Description

From a graph object of class `dgr_graph`, get one or more data frames already bound as node and/or edge attribute values given graph node and/or edges.

### Usage

    get_attr_dfs(graph, node_id = NULL, edge_id = NULL,
      return_format = "single_tbl")

### Arguments

`graph` | a graph object of class `dgr_graph`.
`node_id` | a vector of node ID values in which data frames are bound as node attrs.
`edge_id` | a vector of edge ID values in which data frames are bound as edge attrs.
`return_format` | the format in which to return the results of several data frames. These can either be: (1) `single_tbl` (a tibble object resulting from a 'bind_rows' operation of multiple data frames), and (2) `single_df` (a single data frame which all of the data frame data).

### Value

either a tibble, a data frame, or a list.

### Examples

    # Create a node data frame (ndf)
    ndf <-
      create_node_df(
        n = 4,
        type = "basic",
        label = TRUE,
        value = c(3.5, 2.6, 9.4, 2.7))

    # Create an edge data frame (edf)
    edf <-
      create_edge_df(
        from = c(1, 2, 3),
        to = c(4, 3, 1),
        rel = "leading_to")

    # Create a graph
    graph <-
      create_graph(
        nodes_df = ndf,
        edges_df = edf)

    # Create 3 simple data frames to add as
    # attributes to nodes/edges
    df_1 <-
      data.frame(
        a = c("one", "two"),
        b = c(1, 2),
        stringsAsFactors = FALSE)

    df_2 <-
      data.frame(
        a = c("three", "four"),
        b = c(3, 4),
        stringsAsFactors = FALSE)

    df_for_edges <-
      data.frame(
        c = c("five", "six"),
        d = c(5, 6),
        stringsAsFactors = FALSE)

    # Bind data frames as node attributes
    # for nodes `1` and `4`; bind a data
    # frame as an edge attribute as well
    graph <-
      graph %>%
      set_df_as_node_attr(
        node = 1,
        df = df_1) %>%
      set_df_as_node_attr(
        node = 4,
        df = df_2) %>%
      set_df_as_edge_attr(
        edge = 1,
        df = df_for_edges)

    # Get a single tibble by specifying the
    # nodes from which there are data frames
    # bound as node attributes
    get_attr_dfs(
      graph,
      node_id = c(1, 4))
    #> # A tibble: 4 x 6
    #>   node_edge__    id  type label     a     b
    #>         <chr> <int> <chr> <chr> <chr> <dbl>
    #> 1        node     1 basic     1   one     1
    #> 2        node     1 basic     1   two     2
    #> 3        node     4 basic     4 three     3
    #> 4        node     4 basic     4  four     4

    # You can also get data frames that are
    # associated with edges by using the
    # same function
    get_attr_dfs(
      graph,
      edge_id = 1)
    #> # A tibble: 2 x 7
    #>   node_edge__    id        rel  from    to     c     d
    #>         <chr> <int>      <chr> <int> <int> <chr> <dbl>
    #> 1        edge     1 leading_to     1     4  five     5
    #> 2        edge     1 leading_to     1     4   six     6

    # It's also possible to collect data frames
    # associated with both nodes and edges
    get_attr_dfs(
      graph,
      node_id = 4,
      edge_id = 1)
    #> # A tibble: 4 x 11
    #>   node_edge__    id  type label        rel  from    to
    #>         <chr> <int> <chr> <chr>      <chr> <int> <int>
    #> 1        node     4 basic     4       <NA>    NA    NA
    #> 2        node     4 basic     4       <NA>    NA    NA
    #> 3        edge     1  <NA>  <NA> leading_to     1     4
    #> 4        edge     1  <NA>  <NA> leading_to     1     4
    #> # ... with 4 more variables: a <chr>, b <dbl>, c <chr>,
    #> #   d <dbl>

    # If a data frame is desired instead,
    # set `return_format = "single_df"`
    get_attr_dfs(
      graph,
      edge_id = 1,
      return_format = "single_df")
    #>   node_edge__ id        rel from to    c d
    #> 1        edge  1 leading_to    1  4 five 5
    #> 2        edge  1 leading_to    1  4  six 6

[Package DiagrammeR version 0.9.2 Index]