i want to update node name which i have selected form graph

library(shiny)
library(DiagrammeR)

ui <- fluidPage(
grVizOutput("dg"),
textInput("node_label", "Enter new label"),
actionButton("update_label", "Update label"),
verbatimTextOutput("print")
)

server <- function(input, output, session) {
node_labels <- reactiveVal(list(a = "Start", b = "Step 1", c = "Step 2", d = "Step 3", e = "Step 4", f = "Step 5", g = "Step 6", h = "Step 7", i = "Step 8", j = "Step 9"))

output$dg <- renderGrViz({
grViz(paste0("
digraph a_nice_graph {
# node definitions with substituted label text
node [fontname = Helvetica]
a [label = '", node_labels()$a, "']
b [label = '", node_labels()$b, "']
c [label = '", node_labels()$c, "']
d [label = '", node_labels()$d, "']
e [label = '", node_labels()$e, "']
f [label = '", node_labels()$f, "']
g [label = '", node_labels()$g, "']
h [label = '", node_labels()$h, "']
i [label = '", node_labels()$i, "']
j [label = '", node_labels()$j, "']
# edge definitions with the node IDs
a -> {b c d e f g h i j}
}
"))
})

observeEvent(input$update_label, {
if (!is.null(input$dg_click)) {
node_val <- input$dg_click$nodeValues[[1]]
new_val <- input$node_label
node_labels(node_labels() %>% {.[[node_val]] <- new_val; .})
}
})

txt <- reactive({
req(input$dg_click)
nodeval <- input$dg_click$nodeValues[[1]]
return(paste(nodeval, " is clicked"))
})

output$print <- renderPrint({
txt()
})
}

shinyApp(ui, server)

observeEvent(input$update_label, {
  if (!is.null(input$dg_click)) {
    node_val <- input$dg_click$nodeValues[[1]]
    new_val <- input$node_label
    label_to_change <- names(which(node_labels() == node_val))
    req(label_to_change)

    node_labels(node_labels() %>%
      {
        .[[label_to_change]] <- new_val
        .
      })
  }
})
1 Like

i am creating separate input box for changing node label name can we update node name by clicking on it only without creating input box separately?

I don't understand your idea

In the above code, we update the node label by first selecting it then changing its label name and then updating its name instead of this can we update the node name directly inside of the node?

Oh, I think you misunderstand.
to update the label, we needed to find the name of which label to change. but we didnt change the name its still, a,b,c, d, or whatever it used to be.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.