Tengo la aplicación shiny a continuación en la que uso una técnica js para capturar clics en la aplicación sin tener que usar elementos de entrada brillantes. Funciona muy bien, pero no en los nodos, ya que quiero poder capturar nodos y obtener la ID del nodo.
La lógica es la siguiente: hago clic en el texto "Click me" o en los nodos y luego, en la consola de Rstudio, escribo input$js.node_clicked . El resultado debería ser "uno_1", que puedo aceptar porque "one" corresponde al nodo y "_1" al hacer clic en la secuencia. Pero cuando hago clic en los nodos, obtengo solo "_1" y no la identificación del nodo "one_1". La lógica es de aquí .
library(shiny) library(shinyWidgets) library(DiagrammeR) library(magrittr) js <- ' $(document).on("click", ".node", function(e) { if(typeof BUTTON_CLICK_COUNT == "undefined") { BUTTON_CLICK_COUNT = 1; } else { BUTTON_CLICK_COUNT ++; } Shiny.onInputChange("js.node_clicked", e.target.id + "_" + BUTTON_CLICK_COUNT); }); ' ui <- fluidPage( tags$script(HTML(js)) , div(id="one","click me",class="node") , uiOutput("main") ) server <- function(input, output) { output$main <- renderUI({ tagList( div( grVizOutput("grr", width = "100%", height = "90vh") )) }) output$grr <- renderGrViz(grViz( "digraph test{ A[tooltip='A word']; B[tooltip='Another word']; A -> B;}" ) ) observeEvent(input$js.node_clicked , { browser() }) # } shinyApp(ui, server)Esto funciona así. Tienes que usar currentTarget en lugar de target . Evite incluir un punto en el nombre de entrada de Shiny.setInputValue (o Shiny.onInputChange , que es lo mismo), porque el punto tiene un significado especial.
library(shiny) library(shinyWidgets) library(DiagrammeR) library(magrittr) js <- ' $(document).on("click", ".node", function(e) { if(typeof BUTTON_CLICK_COUNT === "undefined") { BUTTON_CLICK_COUNT = 1; } else { BUTTON_CLICK_COUNT++; } Shiny.setInputValue("js_node_clicked", e.currentTarget.id + "_" + BUTTON_CLICK_COUNT); }); ' ui <- fluidPage( tags$script(HTML(js)), div(id="one", "click me", class="node"), uiOutput("main") ) server <- function(input, output) { output$main <- renderUI({ tagList( div( grVizOutput("grr", width = "100%", height = "90vh") ) ) }) output$grr <- renderGrViz(grViz( "digraph test{ A[tooltip='A word']; B[tooltip='Another word']; A -> B;}" ) ) observeEvent(input$js_node_clicked , { print(input$js_node_clicked) }) } shinyApp(ui, server)