if (interactive()) {
library(shiny)
library(g6R)
library(bslib)
# Static data defined globally
nodes <- data.frame(id = 1:3)
edges <- data.frame(source = c(1, 2), target = c(2, 3))
ui <- page_fluid(
title = "Add Nodes Dynamically",
g6_output("graph"),
actionButton("add_node", "Add Node")
)
server <- function(input, output, session) {
output$graph <- render_g6({
g6(nodes = nodes, edges = edges) |> g6_layout()
})
next_id <- reactiveVal(max(nodes$id) + 1)
observeEvent(input$add_node, {
g6_add_nodes(g6_proxy("graph"), data.frame(id = next_id()))
next_id(next_id() + 1)
})
}
shinyApp(ui, server)
}
Run the code above in your browser using DataLab