### Run Shiny Example for visNetwork
Source: http://datastorm-open.github.io/visNetwork
Launches an example Shiny application demonstrating the use of the visNetwork package.
```r
# shiny example
shiny::runApp(system.file("shiny", package = "visNetwork"))
```
--------------------------------
### Install visNetwork Package
Source: http://datastorm-open.github.io/visNetwork
Installs the visNetwork package from CRAN or the development version from GitHub.
```r
install.packages("visNetwork")
# can have new features in development version
devtools::install_github("datastorm-open/visNetwork")
```
--------------------------------
### Prepare Data for visNetwork
Source: http://datastorm-open.github.io/visNetwork/options.html
Sets up sample data frames for nodes and edges, which are commonly used in visNetwork examples.
```r
# data used in next examples
nb <- 10
nodes <- data.frame(id = 1:nb, label = paste("Label", 1:nb),
group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb,
title = paste0("
", 1:nb,"
Tooltip !
"), stringsAsFactors = FALSE)
edges <- data.frame(from = c(8,2,7,6,1,8,9,4,6,2),
to = c(3,7,2,7,9,1,5,3,2,9),
value = rnorm(nb, 10), label = paste("Edge", 1:nb),
title = paste0("", 1:nb,"
Edge Tooltip !
"))
```
--------------------------------
### Load visNetwork from Gephi JSON Export
Source: http://datastorm-open.github.io/visNetwork/more.html
Initializes a visNetwork graph by loading data from a Gephi JSON export file. Note: This example is commented out and should not be run directly.
```r
# don't run here
visNetwork(gephi = 'WorldCup2014.json')
```
--------------------------------
### Define Node Groups and Styles with VisGroups
Source: http://datastorm-open.github.io/visNetwork/groups.html
Assign nodes to groups using the 'group' column and configure visual properties for each group using visGroups. This example shows how to style group 'A' as dark blue squares with shadows and group 'B' as red triangles.
```r
nodes <- data.frame(id = 1:5, group = c(rep("A", 2), rep("B", 3)))
edges <- data.frame(from = c(2,5,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
# darkblue square with shadow for group "A"
visGroups(groupname = "A", color = "darkblue", shape = "square",
shadow = list(enabled = TRUE)) %>%
# red triangle for group "B"
visGroups(groupname = "B", color = "red", shape = "triangle")
```
--------------------------------
### Running VisNetwork Demonstration Apps
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Launches the built-in demonstration Shiny applications for VisNetwork. This is a good way to explore the package's capabilities.
```R
shiny::runApp(system.file("shiny", package = "visNetwork"))
```
--------------------------------
### Enable Keyboard Navigation and Set Tooltip Delay
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Configure keyboard manipulation for network control and set the delay before tooltips appear. A tooltipDelay of 0 means tooltips appear immediately on hover.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(keyboard = TRUE, tooltipDelay = 0)
```
--------------------------------
### Configure visNetwork with visConfigure()
Source: http://datastorm-open.github.io/visNetwork/configure.html
Use visConfigure() to enable an interactive configuration interface for your network directly in the browser. This is useful for testing and fine-tuning network options.
```R
nodes <- data.frame(id = 1:3, label = LETTERS[1:3])
edges <- data.frame(from = c(1,2), to = c(1,3))
# don't look in RStudio viewer
visNetwork(nodes, edges, width = "100%") %>%
visConfigure(enabled = TRUE)
```
--------------------------------
### Basic Shiny App with VisNetwork
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Integrates a minimal VisNetwork graph into a Shiny app using visNetworkOutput and renderVisNetwork. Ensure 'shiny' and 'visNetwork' packages are loaded.
```R
require(shiny)
require(visNetwork)
server <- function(input, output) {
output$mynetworkid <- renderVisNetwork({
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges)
})
}
ui <- fluidPage(
visNetworkOutput("mynetworkid")
)
shinyApp(ui = ui, server = server)
```
--------------------------------
### Custom Shiny Input from VisNetwork Events
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Demonstrates creating custom Shiny inputs by listening to VisNetwork events (e.g., node hover) using JavaScript's Shiny.onInputChange. This allows passing data from the network to Shiny.
```R
library(visNetwork)
library(shiny)
server <- function(input, output) {
output$network <- renderVisNetwork({
# minimal example
nodes <- data.frame(id = 1:3, label = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>%
visInteraction(hover = TRUE) %>%
visEvents(hoverNode = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes);
;")
})
output$shiny_return <- renderPrint({
input$current_node_id
})
}
ui <- fluidPage(
visNetworkOutput("network"),
verbatimTextOutput("shiny_return")
)
shinyApp(ui = ui, server = server)
```
--------------------------------
### Use igraph layout with visIgraphLayout()
Source: http://datastorm-open.github.io/visNetwork/igraph.html
Demonstrates how to use igraph's layout algorithms to compute node coordinates before rendering with vis.js. This allows for fast rendering using pre-calculated layouts.
```APIDOC
## Use igraph layout
With `visIgraphLayout()`, you can use all available layouts in igraph and calculate coordinates before the sending to vis.js :
```R
nnodes <- 100
nnedges <- 200
nodes <- data.frame(id = 1:nnodes)
edges <- data.frame(from = sample(1:nnodes, nnedges, replace = T),
to = sample(1:nnodes, nnedges, replace = T))
# with defaut layout
visNetwork(nodes, edges, height = "500px") %>%
visIgraphLayout() %>%
visNodes(size = 10)
```
```R
# in circle ?
visNetwork(nodes, edges, height = "500px") %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visNodes(size = 10) %>%
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
```
```
--------------------------------
### Access vis.js Documentation and Vignettes
Source: http://datastorm-open.github.io/visNetwork
Provides access to the JavaScript API documentation for vis.js and the introduction vignette for the visNetwork package.
```r
# javascrtip api
visDocumentation()
vignette("Introduction-to-visNetwork") # with CRAN version
```
--------------------------------
### Additional Interaction Options
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Configure various other interaction behaviors like keyboard manipulation, hover effects, selection, and tooltip delays.
```APIDOC
## And also…
### Description
Configure additional interaction features including keyboard control, hover effects, selection behavior, and tooltip display delays.
### Method
visInteraction()
### Parameters
#### Options
- **keyboard** (boolean) - Enable keyboard manipulation instead of mouse control.
- **hover** (boolean) - Control the hover effect on nodes.
- **hoverConnectedEdges** (boolean) - Control the hover effect on edges connected to hovered nodes.
- **selectable** (boolean) - Disable the selection of nodes and edges.
- **tooltipDelay** (integer) - Set the delay in milliseconds before a tooltip is shown.
### Request Example
```R
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(keyboard = TRUE, tooltipDelay = 0)
```
```
--------------------------------
### Create Basic Classification Tree
Source: http://datastorm-open.github.io/visNetwork/tree.html
Use visTree to visualize a basic classification tree generated by rpart. Ensure the rpart package is loaded.
```R
library(rpart)
# Basic classification tree
res <- rpart(Species~., data=iris)
visTree(res, main = "Iris classification Tree", width = "100%")
```
--------------------------------
### Apply Global Edge Configuration with visEdges
Source: http://datastorm-open.github.io/visNetwork/edges.html
Set default configurations for all edges using the visEdges function. This is useful for applying consistent styling across the network.
```R
nodes <- data.frame(id = 1:4)
edges <- data.frame(from = c(2,4,3,2), to = c(1,2,4,3))
visNetwork(nodes, edges, width = "100%") %>%
visEdges(shadow = TRUE,
arrows =list(to = list(enabled = TRUE, scaleFactor = 2)),
color = list(color = "lightblue", highlight = "red")) %>%
visLayout(randomSeed = 12) # to have always the same network
```
--------------------------------
### Show Navigation Buttons
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Enable navigation buttons for easier control of the network view. These buttons typically allow zooming and panning.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(navigationButtons = TRUE)
```
--------------------------------
### Basic visNetwork Usage
Source: http://datastorm-open.github.io/visNetwork
Creates a basic network visualization using visNetwork. Requires nodes and edges data frames with 'id', 'from', and 'to' columns respectively.
```r
require(visNetwork, quietly = TRUE)
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")
```
--------------------------------
### Configure visNetwork Physics with forceAtlas2Based Solver
Source: http://datastorm-open.github.io/visNetwork/physic.html
Use the visPhysics() function to set the solver to 'forceAtlas2Based' and adjust its specific parameters like gravitationalConstant.
```r
nodes <- data.frame(id = 1:10)
edges <- data.frame(from = round(runif(8)*10), to = round(runif(8)*10))
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visPhysics(solver = "forceAtlas2Based",
forceAtlas2Based = list(gravitationalConstant = -500))
```
--------------------------------
### Add Title, Subtitle, and Footer to VisNetwork
Source: http://datastorm-open.github.io/visNetwork/legend.html
Use the main, submain, and footer arguments to add text elements to your network visualization. The submain can be styled using a list.
```r
nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
edges <- data.frame(from = c(1,2), to = c(2,3))
# default, on group
visNetwork(nodes, edges,
main = "A really simple example",
submain = list(text = "Custom subtitle",
style = "font-family:Comic Sans MS;color:#ff0000;font-size:15px;text-align:center;"),
footer = "Fig.1 minimal example",
width = "100%")
```
--------------------------------
### Global Node Configuration in R
Source: http://datastorm-open.github.io/visNetwork/nodes.html
Apply global configurations to all nodes using the visNodes function. This is useful for setting default shapes, colors, and shadow properties for the entire network. Requires initial node and edge data frames.
```r
nodes <- data.frame(id = 1:4)
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
visNodes(shape = "square",
color = list(background = "lightblue",
border = "darkblue",
highlight = "yellow"),
shadow = list(enabled = TRUE, size = 10)) %>%
visLayout(randomSeed = 12) # to have always the same network
```
--------------------------------
### Create Regression Tree with Specific Control Parameters
Source: http://datastorm-open.github.io/visNetwork/tree.html
Visualize a regression tree with specific control parameters for rpart, such as cp, minNodeSize, and maxNodeSize. The height of the visualization can also be set.
```R
data("solder")
res <- rpart(Opening~., data = solder, control = rpart.control(cp = 0.00005))
visTree(res, height = "800px", nodesPopSize = TRUE, minNodeSize = 10,
maxNodeSize = 30, width = "100%")
```
--------------------------------
### Create visNetwork from DOT Language String
Source: http://datastorm-open.github.io/visNetwork/more.html
Generates a visNetwork graph directly from a string formatted in the DOT language. This allows for easy import of graph descriptions.
```r
visNetwork(dot = 'dinetwork {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }',
width = "100%")
```
--------------------------------
### Configure visNetwork with visNetworkEditor
Source: http://datastorm-open.github.io/visNetwork/configure.html
Utilize the visNetworkEditor Shiny module for an integrated and interactive way to configure your network. This provides a dedicated editor interface within a Shiny application.
```R
nodes <- data.frame(id = 1:3, label = paste("Node", 1:3))
edges <- data.frame(from = c(1,2), to = c(1,3), label = paste("Edge", 1:2))
network <- visNetwork(nodes, edges)
custom_network <- visNetworkEditor(object = network)
custom_network
```
--------------------------------
### Using Font Awesome Icons for Nodes in VisNetwork
Source: http://datastorm-open.github.io/visNetwork/image_icon.html
Set node shape to 'icon' and define icon properties (code, size, color) within a list passed to 'visGroups' or 'visNodes'. Use addFontAwesome() to include the library.
```r
nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
edges <- data.frame(from = c(1,2), to = c(2,3))
visNetwork(nodes, edges, width = "100%") %>%
visGroups(groupname = "A", shape = "icon",
icon = list(code = "f0c0", size = 75)) %>%
visGroups(groupname = "B", shape = "icon",
icon = list(code = "f007", color = "red")) %>%
addFontAwesome()
```
--------------------------------
### Use Complex List-Based Configurations for Edges
Source: http://datastorm-open.github.io/visNetwork/edges.html
Apply advanced, list-based configurations to individual edges, such as font properties and shadow effects. This enables detailed customization of edge appearance.
```R
nodes <- data.frame(id = 1:3,
color.background = c("red", "blue", "green"),
color.highlight.background = c("red", NA, "red"),
shadow.size = c(5, 10, 15))
edges <- data.frame(from = c(1,2), to = c(1,3),
label = LETTERS[1:2],
font.color =c ("red", "blue"),
font.size = c(10,20))
visNetwork(nodes, edges)
```
--------------------------------
### Transform igraph network using visIgraph() or toVisNetworkData()
Source: http://datastorm-open.github.io/visNetwork/igraph.html
Shows how to convert an igraph network object directly into a visNetwork visualization or extract its data in a format compatible with visNetwork.
```APIDOC
## Use igraph network
`visIgraph` directly transforms an igraph network into a **visNetwork**. It’s a little bit experimental (not perfect). You can also use `toVisNetworkData` to just get data into **visNetwork** format :
```R
library("igraph", quietly = TRUE, warn.conflicts = FALSE, verbose = FALSE)
igraph_network <- graph.famous("Walther")
plot(igraph_network)
```
```R
# get data and plot :
data <- toVisNetworkData(igraph_network)
visNetwork(nodes = data$nodes, edges = data$edges, height = "500px")
```
```R
# or plot directly
visIgraph(igraph_network)
```
```
--------------------------------
### Create Regression Tree
Source: http://datastorm-open.github.io/visNetwork/tree.html
Visualize a regression tree using visTree. Customizations for node and edge font sizes are applied.
```R
res <- rpart(Petal.Length~., data=iris)
visTree(res, edgesFontSize = 14, nodesFontSize = 16, width = "100%")
```
--------------------------------
### Configure visNetwork Physics with barnesHut Solver
Source: http://datastorm-open.github.io/visNetwork/physic.html
Use the visPhysics() function to set the solver to 'barnesHut'. This is a common solver for network physics simulations.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visPhysics(solver = "barnesHut")
```
--------------------------------
### Combined Individual and Global Node Config in R
Source: http://datastorm-open.github.io/visNetwork/nodes.html
Combine individual node configurations with global settings applied via visNodes. This allows for specific overrides while maintaining general styles across the network. Ensure node and edge data frames are prepared.
```r
nodes <- data.frame(id = 1:4,
shape = c("circle", "square"),
label = LETTERS[1:4])
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
visNodes(color = list(background = "lightblue",
border = "darkblue",
highlight = "yellow"),
shadow = list(enabled = TRUE, size = 10)) %>%
visLayout(randomSeed = 12) # to have always the same network
```
--------------------------------
### Configure Hierarchical Layout Options
Source: http://datastorm-open.github.io/visNetwork/layout.html
Applies a hierarchical layout with custom direction and level separation. Use this to control the orientation and spacing of hierarchical levels.
```r
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = "from") %>%
visHierarchicalLayout(direction = "LR", levelSeparation = 500)
```
--------------------------------
### Define Nodes and Edges for Network
Source: http://datastorm-open.github.io/visNetwork/layout.html
Sets up the basic data structure for nodes and edges in a visNetwork graph. This is a prerequisite for most network visualizations.
```r
nodes <- data.frame(id = 1:7)
edges <- data.frame(from = c(1,2,2,2,3,3),
to = c(2,3,4,5,6,7))
```
--------------------------------
### Highlight Nearest with Images and Icons
Source: http://datastorm-open.github.io/visNetwork/options.html
Applies highlighting nearest nodes with depth and hover options, while also configuring group appearances with icons. Requires addFontAwesome.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visGroups(groupname = "A", shape = "icon",
icon = list(code = "f0c0", size = 75)) %>%
visGroups(groupname = "B", shape = "icon",
icon = list(code = "f007", color = "red")) %>%
visGroups(groupname = "C", shape = "icon",
icon = list(code = "f1b9", color = "black")) %>%
visOptions(highlightNearest = list(enabled =TRUE, degree = 2, hover = T)) %>%
addFontAwesome() %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Load and Visualize 'Les Miserables' Data with visNetwork
Source: http://datastorm-open.github.io/visNetwork/options.html
Loads 'Les Miserables' node and edge data from JSON files and visualizes it using `visNetwork`. Options include selecting nodes by group and highlighting nearest nodes.
```r
nodes <- jsonlite::fromJSON("https://raw.githubusercontent.com/datastorm-open/datastorm-open.github.io/master/visNetwork/data/nodes_miserables.json")
edges <- jsonlite::fromJSON("https://raw.githubusercontent.com/datastorm-open/datastorm-open.github.io/master/visNetwork/data/edges_miserables.json")
visNetwork(nodes, edges, height = "700px", width = "100%") %>%
visOptions(selectedBy = "group",
highlightNearest = TRUE,
nodesIdSelection = TRUE) %>%
visPhysics(stabilization = FALSE)
```
--------------------------------
### Frozen Network Configuration
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Control whether nodes can be selected and dragged, the entire view can be moved, or the view can be zoomed using the mouse scroll.
```APIDOC
## Frozen network
### Description
Control the ability to select and move nodes, move the entire network view, and zoom the view.
### Method
visInteraction()
### Parameters
#### Options
- **dragNodes** (boolean) - Enable or disable the selection and movement of nodes.
- **dragView** (boolean) - Enable or disable the movement of the full network.
- **zoomView** (boolean) - Enable or disable zooming using the mouse scroll.
### Request Example
```R
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(dragNodes = FALSE,
dragView = FALSE,
zoomView = FALSE) %>%
visLayout(randomSeed = 123)
```
```
--------------------------------
### Highlight Nearest Nodes with Depth and Hover
Source: http://datastorm-open.github.io/visNetwork/options.html
Configures highlighting of nearest nodes with a specified degree of depth and enables highlighting on hover. Clicking still sets the view.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(highlightNearest = list(enabled = T, degree = 2, hover = T)) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Use igraph layout for visNetwork
Source: http://datastorm-open.github.io/visNetwork/igraph.html
Applies igraph's default layout algorithm to compute node coordinates before rendering with visNetwork. Ensure nodes and edges data frames are prepared.
```R
nnodes <- 100
nnedges <- 200
nodes <- data.frame(id = 1:nnodes)
edges <- data.frame(from = sample(1:nnodes, nnedges, replace = T),
to = sample(1:nnodes, nnedges, replace = T))
# with defaut layout
visNetwork(nodes, edges, height = "500px") %>%
visIgraphLayout() %>%
visNodes(size = 10)
```
--------------------------------
### Navigation Buttons
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Display navigation buttons to control the network view.
```APIDOC
## Navigation buttons
### Description
Show navigation buttons to allow users to control the network view.
### Method
visInteraction()
### Parameters
#### Options
- **navigationButtons** (boolean) - Show or hide navigation buttons.
### Request Example
```R
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(navigationButtons = TRUE)
```
```
--------------------------------
### Combine Individual and Global Edge Configurations
Source: http://datastorm-open.github.io/visNetwork/edges.html
Integrate specific edge properties defined in the data frame with global settings applied via visEdges. This allows for a mix of unique and uniform edge styles.
```R
nodes <- data.frame(id = 1:4, label = 1:4)
edges <- data.frame(from = c(2,4,3,2),
to = c(1,2,4,3),
dashes = c(TRUE, FALSE))
visNetwork(nodes, edges, width = "100%") %>%
visEdges(shadow = TRUE,
arrows =list(to = list(enabled = TRUE, scaleFactor = 2)),
color = list(color = "lightblue", highlight = "red")) %>%
visLayout(randomSeed = 12) # to have always the same network
```
--------------------------------
### Apply Default Hierarchical Layout
Source: http://datastorm-open.github.io/visNetwork/layout.html
Applies a default hierarchical layout to the network graph. This is equivalent to using visLayout(hierarchical = TRUE).
```r
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = "from") %>%
visHierarchicalLayout() # same as visLayout(hierarchical = TRUE)
```
--------------------------------
### Using Ionicons for Nodes in VisNetwork
Source: http://datastorm-open.github.io/visNetwork/image_icon.html
Set node shape to 'icon' and specify 'face = "Ionicons"' along with icon properties. Use addIonicons() to include the library. Icon definitions can be directly in the nodes data frame.
```r
nodes <- data.frame(id = 1:3, shape = "icon", icon.face = 'Ionicons',
icon.code = c("f101", "f100", "f101"))
edges <- data.frame(from = c(1,2), to = c(2,3))
visNetwork(nodes, edges) %>%
addIonicons()
```
--------------------------------
### Apply specific igraph layout (circle) in visNetwork
Source: http://datastorm-open.github.io/visNetwork/igraph.html
Renders a visNetwork graph using the 'layout_in_circle' algorithm from igraph. Useful for visualizing network structures in a circular arrangement. Includes options for highlighting nearest nodes and node selection.
```R
# in circle ?
visNetwork(nodes, edges, height = "500px") %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visNodes(size = 10) %>%
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
```
--------------------------------
### Shiny Interactions: Selection by Group
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Shows how to enable selection based on node groups in VisNetwork. The input$mynetworkid_selectedBy will reflect the selected group.
```R
output$mynetwork <- renderVisNetwork({
... visOptions(selectedBy = "group")
})
# created input$mynetworkid_selectedBy
```
--------------------------------
### Add Custom Nodes with Icons to VisLegend using Lists
Source: http://datastorm-open.github.io/visNetwork/legend.html
Define custom nodes for the legend using lists, specifying properties like label, shape, and icon details. Ensure addFontAwesome() is called if using Font Awesome icons.
```r
nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
edges <- data.frame(from = c(1,2), to = c(2,3))
visNetwork(nodes, edges) %>%
visGroups(groupname = "A", shape = "icon",
icon = list(code = "f0c0", size = 75)) %>%
visGroups(groupname = "B", shape = "icon",
icon = list(code = "f007", color = "red")) %>%
addFontAwesome() %>%
visLegend(addNodes = list(
list(label = "Group", shape = "icon",
icon = list(code = "f0c0", size = 25)),
list(label = "User", shape = "icon",
icon = list(code = "f007", size = 50, color = "red"))),
useGroups = FALSE)
```
--------------------------------
### Use igraph Layout for visNetwork Performance
Source: http://datastorm-open.github.io/visNetwork/performance.html
Increase plotting speed by using the igraph library to compute node coordinates before rendering with visNetwork. This leverages igraph's optimized layout algorithms.
```r
visNetwork(nodes, edges) %>%
visIgraphLayout()
```
--------------------------------
### Efficient Network Updates with visNetworkProxy
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Utilizes visNetworkProxy in Shiny to update network elements (nodes, focus) without redrawing the entire graph. This is more performant for dynamic changes.
```R
require(shiny)
require(visNetwork)
server <- function(input, output) {
output$network_proxy_nodes <- renderVisNetwork({
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>% visNodes(color = "blue")
})
observe({
visNetworkProxy("network_proxy_nodes") %>%
visFocus(id = input$Focus, scale = 4)
})
observe({
visNetworkProxy("network_proxy_nodes") %>%
visNodes(color = input$color)
})
}
ui <- fluidPage(
fluidRow(
column(
width = 4,
selectInput("color", "Color :",
c("blue", "red", "green")),
selectInput("Focus", "Focus on node :",
c(1:3))
),
column(
width = 8,
visNetworkOutput("network_proxy_nodes", height = "400px")
)
)
)
shinyApp(ui = ui, server = server)
```
--------------------------------
### Enable Data Manipulation in visNetwork
Source: http://datastorm-open.github.io/visNetwork/options.html
Use `visOptions(manipulation = TRUE)` to enable editing of nodes and edges directly within the network visualization. This requires the `visNetwork` package.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(manipulation = TRUE) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Shiny Interactions: Node Selection
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Demonstrates how to enable node selection in VisNetwork within a Shiny app. When nodesIdSelection is TRUE, input$mynetworkid_selected will contain the ID of the selected node.
```R
output$mynetwork <- renderVisNetwork({
... visOptions(nodesIdSelection = TRUE)
})
# created input$mynetworkid_selected
```
--------------------------------
### Directly plot igraph network with visIgraph
Source: http://datastorm-open.github.io/visNetwork/igraph.html
Uses the `visIgraph` function to directly render an igraph network object within visNetwork. This is a more experimental approach for direct transformation.
```R
# or plot directly
visIgraph(igraph_network)
```
--------------------------------
### Highlight Nearest Nodes on Click
Source: http://datastorm-open.github.io/visNetwork/options.html
Enables highlighting of the nearest nodes and edges when a node is clicked. Clicking elsewhere on the network resets the highlighting.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(highlightNearest = TRUE) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Cluster Nodes in visNetwork
Source: http://datastorm-open.github.io/visNetwork/more.html
Applies clustering algorithms to group nodes based on color, connection, hub size, or group assignment. Clusters can be opened by double-clicking.
```r
nodes <- data.frame(id = 1:10, label = paste("Label", 1:10),
group = sample(c("A", "B"), 10, replace = TRUE))
edges <- data.frame(from = c(2,5,10), to = c(1,2,10))
visNetwork(nodes, edges, height = "400px", width = "100%") %>%
visGroups(groupname = "A", color = "red", shape = "square") %>%
visGroups(groupname = "B", color = "yellow", shape = "triangle") %>%
visClusteringByColor(colors = c("red")) %>%
visClusteringByGroup(groups = c("B")) %>%
visLegend()
```
--------------------------------
### Enable Node ID Selection
Source: http://datastorm-open.github.io/visNetwork/options.html
Activates a dropdown list for selecting nodes by their ID or label, in addition to the highlight nearest functionality.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Using Images for Nodes in VisNetwork
Source: http://datastorm-open.github.io/visNetwork/image_icon.html
Set node shape to 'image' or 'circularImage' and provide the image path in the 'image' column. The 'brokenImage' option handles missing images, and 'shapeProperties' with 'useBorderWithImage' can add borders.
```r
path_to_images <- "https://raw.githubusercontent.com/datastorm-open/datastorm-open.github.io/master/visNetwork/data/img/indonesia/"
nodes <- data.frame(id = 1:4,
shape = c("image", "circularImage"),
image = paste0(path_to_images, 1:4, ".png"),
label = "I'm an image")
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
visNodes(shapeProperties = list(useBorderWithImage = TRUE)) %>%
visLayout(randomSeed = 2)
```
--------------------------------
### Create Legend Based on Groups with VisLegend
Source: http://datastorm-open.github.io/visNetwork/legend.html
Generate a legend automatically based on the defined node groups by calling visLegend() after setting up visGroups.
```r
# default, on group
visNetwork(nodes, edges, width = "100%") %>%
visGroups(groupname = "A", color = "red") %>%
visGroups(groupname = "B", color = "lightblue") %>%
visLegend()
```
--------------------------------
### Add Custom Nodes and Edges to VisLegend using DataFrames (Commented Out)
Source: http://datastorm-open.github.io/visNetwork/legend.html
An alternative method to add custom nodes and edges to the legend using data frames, shown in a commented-out block. This approach requires specifying icon properties directly in the data frame.
```r
# # using a data.frame
# addNodes <- data.frame(label = c("Group", "User"), shape = "icon",
# icon.code = c("f0c0", "f007"), icon.size = c(25, 50),
# icon.color = c(NA, "red"))
#
# visNetwork(nodes, edges) %>%
# visGroups(groupname = "A", shape = "icon",
# icon = list(code = "f0c0", size = 75)) %>%
# visGroups(groupname = "B", shape = "icon",
# icon = list(code = "f007", color = "red")) %>%
# addFontAwesome() %>%
# visLegend(addNodes = addNodes,
# addEdges = data.frame(label = "link"), useGroups = FALSE)
```
--------------------------------
### Enable Node Collapse/Uncollapse
Source: http://datastorm-open.github.io/visNetwork/options.html
Allows nodes to be collapsed and uncollapsed, useful for managing complex networks. Edges are directed with arrows pointing to the target node.
```r
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
group = sample(LETTERS[1:3], 15, replace = TRUE))
edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
to = trunc(runif(15)*(15-1))+1)
# keeping all parent node attributes
visNetwork(nodes, edges) %>% visEdges(arrows = "to") %>%
visOptions(collapse = TRUE)
```
--------------------------------
### Individual Node Configuration in R
Source: http://datastorm-open.github.io/visNetwork/nodes.html
Define node properties like id, label, group, value, shape, title, color, and shadow directly within a data frame for individual node customization. Ensure 'id' is present. See ?visNodes for all available options.
```r
nodes <- data.frame(id = 1:10,
# add labels on nodes
label = paste("Node", 1:10),
# add groups on nodes
group = c("GrA", "GrB"),
# size adding value
value = 1:10,
# control shape of nodes
shape = c("square", "triangle", "box", "circle", "dot", "star",
"ellipse", "database", "text", "diamond"),
# tooltip (html or character), when the mouse is above
title = paste0("", 1:10,"
Node !
"),
# color
color = c("darkred", "grey", "orange", "darkblue", "purple"),
# shadow
shadow = c(FALSE, TRUE, FALSE, TRUE, TRUE))
# head(nodes)
# id label group value shape title color shadow
# 1 Node 1 GrA 1 square 1
Node !
darkred FALSE
# 2 Node 2 GrB 2 triangle 2
Node !
grey TRUE
edges <- data.frame(from = c(1,2,5,7,8,10), to = c(9,3,1,6,4,7))
```
```r
visNetwork(nodes, edges, height = "500px", width = "100%")
```
--------------------------------
### Select Nodes by Multiple Column Values
Source: http://datastorm-open.github.io/visNetwork/options.html
Enables selection of nodes by multiple values from a specified column, useful when a column contains comma-separated categories.
```r
# or new column, with multiple groups
nodes$sample <- paste(sample(LETTERS[1:3], nrow(nodes), replace = TRUE),
sample(LETTERS[1:3], nrow(nodes), replace = TRUE),
sep = ",")
nodes$label <- nodes$sample # for see groups
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(selectedBy = list(variable = "sample", multiple = T)) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Apply Hierarchical Layout without Explicit Levels
Source: http://datastorm-open.github.io/visNetwork/layout.html
Applies a hierarchical layout when nodes do not have explicit levels defined. The layout algorithm will determine the levels automatically.
```r
nodes$level <- NULL
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = "from") %>%
visHierarchicalLayout() # same as visLayout(hierarchical = TRUE)
```
--------------------------------
### Freeze Network Interactions
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Disable node dragging, view dragging, and zooming to freeze the network. Useful for static visualizations.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(dragNodes = FALSE,
dragView = FALSE,
zoomView = FALSE) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Select Nodes by Column Value
Source: http://datastorm-open.github.io/visNetwork/options.html
Allows selection of nodes based on the values in a specified column, such as 'group'. Nodes can belong to multiple groups if the column contains comma-separated values.
```r
# on "authorised" column
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(selectedBy = "group") %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Save visNetwork to HTML File
Source: http://datastorm-open.github.io/visNetwork/more.html
Saves a visNetwork object to an HTML file using `visSave()` or `htmlwidgets::saveWidget()`. This is useful for sharing or embedding the network visualization.
```r
network <- visNetwork(nodes, edges, width = "100%")
network %>% visSave(file = "network.html")
# same as
visSave(network, file = "network.html")
# or
htmlwidgets::saveWidget(network, "network.html")
```
--------------------------------
### Position and Title VisLegend
Source: http://datastorm-open.github.io/visNetwork/legend.html
Control the placement (left or right) and width of the legend, and add a title to it using the position, width, and main arguments in visLegend.
```r
visNetwork(nodes, edges, width = "100%") %>%
visGroups(groupname = "A", color = "red") %>%
visGroups(groupname = "B", color = "lightblue") %>%
visLegend(width = 0.1, position = "right", main = "Group")
```
--------------------------------
### Transform igraph network to visNetwork data
Source: http://datastorm-open.github.io/visNetwork/igraph.html
Converts an igraph network object into a format suitable for visNetwork using `toVisNetworkData`. This allows for visualization of igraph networks within the visNetwork framework. Requires the 'igraph' library.
```R
library("igraph", quietly = TRUE, warn.conflicts = FALSE, verbose = FALSE)
igraph_network <- graph.famous("Walther")
plot(igraph_network)
```
```R
# get data and plot :
data <- toVisNetworkData(igraph_network)
visNetwork(nodes = data$nodes, edges = data$edges, height = "500px")
```
--------------------------------
### Use Groups with Custom Edges in VisLegend
Source: http://datastorm-open.github.io/visNetwork/legend.html
When useGroups is TRUE (default), visLegend uses the defined groups. You can still add custom edges using addEdges.
```r
ledges <- data.frame(color = c("lightblue", "red"),
label = c("reverse", "depends"), arrows =c("to", "from"))
visNetwork(nodes, edges, width = "100%") %>%
visGroups(groupname = "A", color = "red") %>%
visGroups(groupname = "B", color = "lightblue") %>%
visLegend(addEdges = ledges, useGroups = TRUE)
```
--------------------------------
### Customize Edges Individually with a Data Frame
Source: http://datastorm-open.github.io/visNetwork/edges.html
Define individual edge properties by adding columns to a data frame. This method allows for fine-grained control over each edge's appearance and behavior.
```R
edges <- data.frame(from = sample(1:10,8), to = sample(1:10, 8),
# add labels on edges
label = paste("Edge", 1:8),
# length
length = c(100,500),
# width
width = c(4,1),
# arrows
arrows = c("to", "from", "middle", "middle;to"),
# dashes
dashes = c(TRUE, FALSE),
# tooltip (html or character)
title = paste("Edge", 1:8),
# smooth
smooth = c(FALSE, TRUE),
# shadow
shadow = c(FALSE, TRUE, FALSE, TRUE))
nodes <- data.frame(id = 1:10, group = c("A", "B"))
```
```R
visNetwork(nodes, edges, height = "500px", width = "100%")
```
--------------------------------
### Add Custom Nodes and Edges to VisLegend
Source: http://datastorm-open.github.io/visNetwork/legend.html
Customize the legend by adding specific nodes and edges using the addNodes and addEdges arguments in visLegend. Set useGroups = FALSE to disable group-based legend items.
```r
# nodes data.frame for legend
lnodes <- data.frame(label = c("Group A", "Group B"),
shape = c( "ellipse"), color = c("red", "lightblue"),
title = "Informations", id = 1:2)
# edges data.frame for legend
ledges <- data.frame(color = c("lightblue", "red"),
label = c("reverse", "depends"), arrows =c("to", "from"))
visNetwork(nodes, edges, width = "100%") %>%
visGroups(groupname = "A", color = "red") %>%
visGroups(groupname = "B", color = "lightblue") %>%
visLegend(addEdges = ledges, addNodes = lnodes, useGroups = FALSE)
```
--------------------------------
### Add JavaScript Event to visNetwork
Source: http://datastorm-open.github.io/visNetwork/more.html
Attaches a JavaScript event listener for node selection. The `visEvents()` function allows custom JavaScript code to be executed when specific network events occur.
```r
nodes <- data.frame(id = 1:3, label = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>%
visEvents(selectNode = "function(properties) {
alert('selected nodes ' + this.body.data.nodes.get(properties.nodes[0]).id);}")
```
--------------------------------
### Performance Optimizations: Hide Elements on Drag
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Improve performance by hiding edges or nodes when the user is dragging the view.
```APIDOC
## Hide edges/nodes on drag
### Description
Enhance performance by hiding edges and/or nodes when the user is dragging the view.
### Method
visInteraction()
### Parameters
#### Options
- **hideEdgesOnDrag** (boolean) - Hide edges when dragging the view.
- **hideNodesOnDrag** (boolean) - Hide nodes when dragging the view.
### Request Example
```R
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(hideEdgesOnDrag = TRUE) %>%
visLayout(randomSeed = 123)
```
```
--------------------------------
### Hide Edges on Drag
Source: http://datastorm-open.github.io/visNetwork/interaction.html
Improve performance by hiding edges when the user drags the view. This can be combined with hiding nodes on drag.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visInteraction(hideEdgesOnDrag = TRUE) %>%
visLayout(randomSeed = 123)
```
--------------------------------
### Shiny Interactions: Manipulation
Source: http://datastorm-open.github.io/visNetwork/shiny.html
Enables network manipulation features in VisNetwork. When manipulation is TRUE, input$mynetworkid__graphChange will provide information about graph changes.
```R
output$mynetwork <- renderVisNetwork({
... visOptions(manipulation = TRUE)
})
# created input$mynetworkid__graphChange
```
--------------------------------
### Define Nodes with Explicit Levels
Source: http://datastorm-open.github.io/visNetwork/layout.html
Defines nodes with an explicit 'level' attribute to control their position in a hierarchical layout. This allows for precise control over the graph's structure.
```r
nodes <- data.frame(id = 1:4, level = c(2, 1, 1, 1))
edges <- data.frame(from = c(1, 1, 1),
to = c(2,3,4))
```
--------------------------------
### Disable Physics Stabilization in visNetwork
Source: http://datastorm-open.github.io/visNetwork/performance.html
Disable or control the physics stabilization process when coordinates are not provided. This can significantly speed up initial rendering.
```r
visNetwork(nodes, edges) %>%
visPhysics(stabilization = FALSE)
```
--------------------------------
### Disable Smooth Edges in visNetwork
Source: http://datastorm-open.github.io/visNetwork/performance.html
Improve performance by disabling the smooth curve rendering for edges. This is a simple yet effective optimization.
```r
visNetwork(nodes, edges) %>%
visEdges(smooth = FALSE)
```
--------------------------------
### Customize Node ID Selection
Source: http://datastorm-open.github.io/visNetwork/options.html
Customizes the appearance and behavior of the node ID selection dropdown, including pre-selecting an item, filtering available values, and applying CSS styles.
```r
visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visOptions(highlightNearest = TRUE,
nodesIdSelection = list(enabled = TRUE,
selected = "8",
values = c(5:10),
style = 'width: 200px; height: 26px;
background: #f8f8f8;
color: darkblue;
border:none;
outline:none;')) %>%
visLayout(randomSeed = 123)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.