networkD3 和 Shiny - 按节点数过滤

networkD3 and Shiny - filter by number of nodes

我有这个闪亮的应用程序,可以从 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)

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

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

有什么想法吗?

我也试过使用 reactiveValues,但不行。我不知道在这种情况下我是否必须对原始数据帧进行子集化并生成网络,或者只是对 forcenetwork 中使用的 dfs 进行子集化(我想我做了但仍然没有工作。)

创建数据后,需要过滤 edges_d3nodes_d3 数据框,然后需要重新调整 fromto 过滤后的 edges_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)