if (interactive()) {
library(shiny)
library(g6R)
library(bslib)
color_to_hex <- function(col) {
rgb <- col2rgb(col)
sprintf("#%02X%02X%02X", rgb[1], rgb[2], rgb[3])
}
nodes <- data.frame(id = c("node1", "node2", "node3"))
edges <- data.frame(source = "node1", target = "node2")
ui <- page_fluid(
g6_output("graph"),
actionButton("add_hull", "Add Hull Plugin"),
selectInput(
"hull_color",
"Hull Color",
choices = c("red", "blue", "green", "orange", "purple"),
selected = "red"
)
)
server <- function(input, output, session) {
output$graph <- render_g6({
g6(nodes = nodes, edges = edges) |>
g6_layout() |>
g6_options(animation = FALSE)
})
observeEvent(input$add_hull, {
g6_add_plugin(
g6_proxy("graph"),
hull(
members = c("node1", "node2", "node3"),
fill = color_to_hex(input$hull_color),
fillOpacity = 0.2
)
)
})
observeEvent(input$hull_color, {
# Only update if hull plugin exists
g6_update_plugin(
g6_proxy("graph"),
key = "hull",
fill = color_to_hex(input$hull_color),
stroke = color_to_hex(input$hull_color)
)
})
}
shinyApp(ui, server)
}
Run the code above in your browser using DataLab