# create-graph-from-datatable

## Description

<mark style="color:purple;">`create-graph-from-datatable`</mark> *adaptor creates a graph structure from a datatable containing edge data*.

Requires selection of two columns from a datatable - a source and target column.\
By default, directed is FALSE, which creates a graph with no directionality to the connections. When changed to TRUE, the directionality is set as going from the 'source' to 'target' column.

## Inputs

**`data`**\
Type: `datatable`\
Required: Yes\
The datatable containing edge data.

**`from column`**\
Type: `text`\
Required: Yes\
The name of the column containing the origin node of the edge.

**`to column`**\
Type: `text`\
Required: Yes\
The name of the column containing the destination node of the edge.

**`directed`**\
Type: `boolean`\
Required: No\
Specifies whether the graph is directed or not. If unspecified, defaults to `False`.

## Outputs

**`graph`**\
Type: `graph`\
A graph structure defining nodes and edges.

## Examples

### Example 1: Default behaviour.

#### Inputs:

`data`:

| id | from | to |
| -- | ---- | -- |
| 1  | A    | B  |
| 2  | B    | C  |
| 3  | C    | A  |
| 4  | C    | D  |

`from column:` from

`to column:` to

`directed:` false

#### Outputs:

`graph`:

```
{ 
    "nodes": [ 
        { "id": "A" }, 
        { "id": "B" }, 
        { "id": "C" }, 
        { "id": "D" } 
    ], 
    "edges": [ 
        { "id": "edge-1", "from": "A", "to": "B", "direction": "none", "attributes": {} }, 
        { "id": "edge-2", "from": "B", "to": "C", "direction": "none", "attributes": {} }, 
        { "id": "edge-3", "from": "C", "to": "A", "direction": "none", "attributes": {} }, 
        { "id": "edge-4", "from": "C", "to": "D", "direction": "none", "attributes": {} } 
    ] 
} 
```

-> Created an undirected graph from the datatable.

### Example 2: Create a directed graph.

#### Inputs:

`data`:

| id | from | to |
| -- | ---- | -- |
| 1  | A    | B  |
| 2  | B    | C  |
| 3  | C    | A  |
| 4  | C    | D  |

`from column:` from

`to column:` to

`directed:`true

#### Outputs:

`graph`:

```
{ 
    "nodes": [ 
    { "id": "A" }, 
    { "id": "B" }, 
    { "id": "C" }, 
    { "id": "D" } 
    ], 
    "edges": [ 
        { "id": "edge-1", "from": "A", "to": "B", "direction": "forward", "attributes": {} }, 
        { "id": "edge-2", "from": "B", "to": "C", "direction": "forward", "attributes": {} }, 
        { "id": "edge-3", "from": "C", "to": "A", "direction": "forward", "attributes": {} }, 
        { "id": "edge-4", "from": "C", "to": "D", "direction": "forward", "attributes": {} } 
        ] 
} 
```

-> Created a directed graph from the datatable.

## Use Cases

* Converting a datatable to a graph, which can then be converted to DOT format [<mark style="color:purple;">`export-graph-to-dot-file`</mark>](https://cgps.gitbook.io/data-flo/reference-guide/export-graph-to-dot-file), which can then be fed into the network argument in the [<mark style="color:purple;">`export-to-microreact-project`</mark>](https://cgps.gitbook.io/data-flo/reference-guide/export-to-microreact-project) adaptor.
* Formatting the data from a spreadsheet before exporting as a DOT file (using [<mark style="color:purple;">`export-graph-to-dot-file`</mark>](https://cgps.gitbook.io/data-flo/reference-guide/export-graph-to-dot-file) adaptor)
* Preparing data for the [<mark style="color:purple;">`apply-force-directed-layout`</mark>](https://cgps.gitbook.io/data-flo/reference-guide/apply-force-directed-layout).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cgps.gitbook.io/data-flo/reference-guide/create-graph-from-datatable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
