I have a networkd3 Sankey diagram in my R/Shiny application, which pulls data from a MySQL connection. I am trying to find a way to 'drill-down' into the chart - click on a node and this will return the name of the node. I will then use this to pass another query to the DB which will populate a table in the application.
Here's what I have -
san <- sankeyNetwork(Links = sanData, Nodes = nodes, Source = "source",
Target = "target", Value = "value")
This plots the chart which works just fine.
Here's what I've tried so far -
Use R package htmlwidgets
, and run the followning JS script using onRender on my sankey chart.
Placeholder for the JS script:
clickFun <- 'd3.selectAll(".link").on("click",function(d) {alert(d.value);})'
In my sankey output:
onRender(san, clickFun)
This returns the value of the link as an alert. I tried using ".node"
and d.data.name
as suggested by some other examples, but I was never able to get this to work and no alert pops up.
Here are my questions:
1. How can I retrieve the name of the node from the sankey plot on mouse click?
2. Assuming I've achieved the above, how can I use the value returned by my JS script in my shiny application? I will change the alert()
bit to return()
, but I am at a loss as to how I can use this in my server application.