gpt4 book ai didi

r - 从没有 actionButton 的 visNetwork 图中获取选定的节点数据

转载 作者:行者123 更新时间:2023-12-04 13:15:50 24 4
gpt4 key购买 nike

我使用了此 question 中给出的示例但我想调整它以显示已选择节点的数据(不是所有节点,但只有这个节点)并且也不使用操作按钮,以便在我单击节点时立即显示数据。

我尝试了很多解决方案都没有成功。

当我创建图表时,我会上传一个 CSV 文件并将其他参数关联到节点(大小、标题...)。当我选择一个节点时是否也可以显示这些参数:

节点$公司名称

节点$company_postcode

节点$数量.大小

...

这是我开始使用的代码,@xclotet 友情提供

require(shiny)
require(visNetwork)

server <- function(input, output, session) {
nodes <- data.frame(id = 1:3,
name = c("first", "second", "third"),
extra = c("info1", "info2", "info3"))
edges <- data.frame(from = c(1,2), to = c(1,3), id= 1:2)

output$network_proxy <- renderVisNetwork({
visNetwork(nodes, edges)
})


output$nodes_data_from_shiny <- renderDataTable({
if(!is.null(input$network_proxy_nodes)){
info <- data.frame(matrix(unlist(input$network_proxy_nodes), ncol = dim(nodes)[1],
byrow=T),stringsAsFactors=FALSE)
colnames(info) <- colnames(nodes)
info
}
})

observeEvent(input$getNodes,{
visNetworkProxy("network_proxy") %>%
visGetNodes()
})
}

ui <- fluidPage(
visNetworkOutput("network_proxy", height = "400px"),
dataTableOutput("nodes_data_from_shiny"),
actionButton("getNodes", "Nodes")
)

shinyApp(ui = ui, server = server)

最佳答案

提出另一个建议,因为上面的建议似乎没有那么优雅地工作:

library(shiny)
library(visNetwork)
library(DT)

server <- function(input, output, session) {
nodes <- data.frame(id = 1:3,
name = c("first", "second", "third"),
extra = c("info1", "info2", "info3"))
edges <- data.frame(from = c(1,2), to = c(1,3), id = 1:2)

output$network_proxy <- renderVisNetwork({
visNetwork(nodes, edges) %>%
visEvents(select = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
})


myNode <- reactiveValues(selected = '')

observeEvent(input$current_node_id, {
myNode$selected <<- input$current_node_id
})

output$table <- renderDataTable({
nodes[which(myNode$selected == nodes$id),]
})

output$dt_UI <- renderUI({
if(nrow(nodes[which(myNode$selected == nodes$id),])!=0){
dataTableOutput('table')
} else{}

})


}

ui <- fluidPage(
visNetworkOutput("network_proxy", height = "400px"),
dataTableOutput("nodes_data_from_shiny"),
uiOutput('dt_UI')
)

shinyApp(ui = ui, server = server)

关于r - 从没有 actionButton 的 visNetwork 图中获取选定的节点数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41018899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com