multilayer_to_json: Export a multilayer network to JSON

View source: R/multilayer_to_json.R

multilayer_to_jsonR Documentation

Export a multilayer network to JSON

Description

Converts an EMLN multilayer object to JSON format compatible with the Multilayer Network Visualizer web app.

Usage

multilayer_to_json(multilayer, file = NULL, bipartite = FALSE, directed = NULL)

Arguments

multilayer

A multilayer object (created by create_multilayer_network or load_emln).

file

Optional file path to write the JSON to. If NULL, returns the JSON string.

bipartite

Logical. Whether the network is bipartite. Defaults to FALSE. The visualizer no longer auto-detects bipartite structure — you must set this to TRUE explicitly. When TRUE, the nodes table must contain a node_type (or legacy node_group) column with exactly two distinct values across the network. Any node_group column is renamed to node_type in the output regardless of the bipartite flag (canonical name for the visualizer).

directed

Logical or NULL. Whether the network is directed. If NULL (default), auto-detected by checking edge list symmetry.

Details

The JSON output contains four arrays matching the visualizer's expected format:

  • nodes: Physical nodes with node_id, node_name, and any extra attributes. For bipartite networks, node_group is mapped to node_type.

  • layers: Layer metadata with layer_id, layer_name, and extra attributes. For bipartite networks, a bipartite: true flag is added.

  • extended: The extended edge list with layer_from, node_from, layer_to, node_to, weight, and any link attributes. For directed networks, a directed: true flag is added to each link.

  • state_nodes: State node map with layer_id, node_id, layer_name, node_name.

Value

If file is NULL, returns the JSON string invisibly. If file is specified, writes JSON to disk and returns the file path invisibly.

Note

The Multilayer Network Visualizer that this function targets is currently in beta. Feedback and bug reports are welcome at https://github.com/Ecological-Complexity-Lab/emln/issues.

See Also

plot_multilayer, create_multilayer_network, load_emln

Examples


# Export to file
net <- load_emln(14)
multilayer_to_json(net, file = "tests/my_network.json", bipartite = TRUE)

# Get JSON string
json_str <- multilayer_to_json(net)


emln documentation built on May 6, 2026, 1:06 a.m.