networkD3和Shiny - 按节点数过滤

问题描述 投票:0回答:1

我有这个闪亮的应用程序,从df生成网络图。

library(shiny)
library(dplyr)
library(tibble)
library(networkD3)

ui <- fluidPage(
  sidebarPanel(
    fluidRow(selectInput("nos","Mínimo de orientações",c(1:10),selected=c(1)))
    ),
  fluidRow(simpleNetworkOutput(
    "redes", width = "100%", height = "800px"
  ))
)
server <- function(input, output, session) {
  df_orientadores <- data.frame(orientador=c("Chet Baker","Bill Evans","Miles Davis","Miles Davis","Dizzy Gillespie","Miles Davis"),
                                autor=c("Clifford Brown","Freddie Hubbard","Kenny Dorham","Kenny Burrell","Arturo Sandoval","Goku"))
  output$redes <- renderSimpleNetwork({
    sources <- df_orientadores %>%
      select(orientador) %>%
      dplyr::rename(label = orientador)
    destination <- df_orientadores %>%
      select(autor) %>%
      dplyr::rename(label = autor)

    nodes <- full_join(sources, destination, by = "label")
    nodes <- nodes %>% group_by(label) %>% count(label) %>% rename(freq=n)

    nodes <- nodes %>% rowid_to_column("id")
    nodes$peso <- ((nodes$freq)^3)

    orientacoes_network <- df_orientadores %>%  
      group_by(orientador, autor) %>%
      dplyr::summarise(weight = n()) %>% 
      ungroup()
    edges <- orientacoes_network %>% 
      left_join(nodes, by = c("orientador" = "label")) %>% 
      dplyr::rename(from = id)

    edges <- edges %>% 
      left_join(nodes, by = c("autor" = "label")) %>% 
      dplyr::rename(to = id)
    edges <- select(edges, from, to, weight)
    nodes_d3 <- mutate(nodes, id = id - 1)
    edges_d3 <- mutate(edges, from = from - 1, to = to - 1) 
    filtro_nos <- nodes_d3

    edges_d3$value <- 1  
    forceNetwork(Links = edges_d3, Nodes = nodes_d3, Source = "from", Target = "to", 
                 NodeID = "label", Group = "id", Value = "value", 
                 opacity = 1, fontSize = 20, zoom = TRUE, Nodesize = "peso",
                 arrows = TRUE)
  })
}
shinyApp(ui, server)

我想通过用户选择的最小数量的节点(在freq数据帧中描述为nodes_d3)更新图形(在input$nos上)

我已经尝试按频率数过滤nodes_d3edges_d3,但它返回错误Warning: Error in $<-.data.frame: replacement has 1 row, data has 0 [No stack trace available]

任何想法怎么做?

我也尝试过使用reactiveValues,但它不会这样做。我不知道在这种情况下我是否需要对原始数据帧进行子集化并生成网络,或者简单地对forcenetwork中使用的dfs进行子集化(我认为我做过但仍然无效)。

r shiny htmlwidgets networkd3
1个回答
0
投票

一旦创建了数据,就需要过滤edges_d3nodes_d3数据帧,然后需要重新调整过滤后的from数据帧中的toedges_d3值,以反映它们引用的节点的新位置在nodes_d3数据框中。

# determine the nodes that have at least the minimum freq
nodes_d3_min_freq <-
  nodes_d3 %>% 
  filter(freq >= input$nos)

# filter the edge list to contain only links to or from the nodes that have
# the minimum or more freq
edges_d3_filtered <-
  edges_d3 %>% 
  filter(from %in% nodes_d3_min_freq$id | to %in% nodes_d3_filtered$id)

# filter the nodes list to contain only nodes that are in or are linked to 
# nodes in the filtered edge list
nodes_d3_filtered <-
  nodes_d3 %>% 
  filter(id %in% unlist(select(edges_d3_filtered, from, to)))

# re-adjust the from and to values to reflect the new positions of nodes in
# the filtered nodes list
edges_d3_filtered$from <- match(edges_d3_filtered$from, nodes_d3_filtered$id) - 1
edges_d3_filtered$to <- match(edges_d3_filtered$to, nodes_d3_filtered$id) - 1

forceNetwork(Links = edges_d3_filtered, Nodes = nodes_d3_filtered, 
             Source = "from", Target = "to", NodeID = "label", 
             Group = "id", Value = "value", opacity = 1, fontSize = 20, 
             zoom = TRUE, Nodesize = "peso", arrows = TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.