将节点信息从networkd3提取到闪亮的反应变量 [英] Extracting node information from networkd3 to a reactive variable in shiny
问题描述
我很难在Shiny中使用networkd3软件包。
我绘制了一个图表,并希望以反应方式将节点信息提取回闪亮,以显示有关某个节点的更多信息。
E.g中心节点父亲和其他Son1...SonN的星图。点击Son节点后,我希望能够以闪亮的方式访问Son1名称,从闪亮的数据框中提取信息,并可视化其他信息(例如工作,年龄等)。
我能够使用clickAction例如在屏幕上显示消息:
I am having a hard time working with networkd3 package in Shiny. I have plotted a graph and would like to extract node information back to shiny in a reactive manner to visualize more information concerning a certain node. E.g star graph with center node "Father" and other "Son1"..."SonN". Upon clicking on the Son node I would like to be able to access the "Son1" name in shiny to extract information from a data frame within shiny and visualize additional information (e.g job, age and so on). I was able to use clickAction to for example show a message on the screen:
forceNetwork(....., clickAction= 'alert(d.name)')
但我不知道怎么弄这个d.name值回到闪亮,所以我可以进一步使用它,并且我开始运行闪亮时没有得到错误,因为d.name不存在从乞讨(可能在这里观察函数?)我基本上需要相当于DT包输入$ table_rows_selected
。
but I do not know how to get this d.name value back into shiny, so I can use it further and that I do not get an error when starting to run shiny, since the d.name does not exist from the begning (maybe observe function here?) I basically need an equivalent of DT package input$table_rows_selected
.
推荐答案
这是真的粗略,但有效, networkD3
forceNetwork
示例点击返回值(使用 clickAction ='Shiny.onInputChange(id,d。name)'
),然后用于显示具有该名称的数据框。
Here's a really rough, but working, example of a networkD3
forceNetwork
plot returning a value on click (using clickAction = 'Shiny.onInputChange("id", d.name)'
), which is then used to display a data frame with that name.
library(shiny)
library(networkD3)
links <- read.table(header = T, text = '
source target value
0 1 1
0 2 1
')
nodes <- read.table(header = T, text = '
name group
dad 1
son1 1
son2 1
')
son1 <- read.table(text = '
name John
age 18
')
son2 <- read.table(text = '
name Mark
age 14
')
ui <- shinyUI(fluidPage(
fluidRow(
column(4, forceNetworkOutput("force")),
column(4, DT::dataTableOutput("table"))
)
))
server <- shinyServer(function(input, output) {
output$force <- renderForceNetwork({
forceNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 1, opacityNoHover = 1,
clickAction = 'Shiny.onInputChange("id", d.name)')
})
output$table <- DT::renderDataTable(DT::datatable(get(input$id)))
})
shinyApp(ui = ui, server = server)
这篇关于将节点信息从networkd3提取到闪亮的反应变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!