首页 > 解决方案 > 闪亮中的零索引问题和过滤问题

问题描述

我正在尝试使用交互式网络构建一个闪亮的应用程序,但我遇到了两个问题。

首先,我无法弄清楚为什么我确实有零索引问题,因为我使用了解决方案来修复它,但它不能正常工作。另一方面,我可能做错了什么。

其次,我有一个基于组(list_initiators$factions)过滤节点的问题。目前,当我想更改“连接”时,图表会做出反应,但它与派系有一些故障。我希望图表只显示派系中的节点,这些派系确实有联系。当我取消选中侧框中的某些派系时,当没有连接并且没有正确反应时,图表会冻结。您也可以在表格中看到这一点。

这是我的代码

library(shiny)
library(dplyr)
library(tidyr)
library(networkD3)

list_initiators <- read.csv("https://raw.githubusercontent.com/Okssana/shiny_app_network/master/nodes_amends_20_09_2020.csv", fileEncoding = "Windows-1251") %>%
  select(-X)

edges_for_gephy <- read.csv("https://raw.githubusercontent.com/Okssana/shiny_app_network/master/edges_amends_20_09_2020.csv") %>% 
  select(-X)


# UI ####
ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      checkboxGroupInput('factions_input', 
                         'Choose faction',
                         choices = unique(list_initiators$factions),
                         selected = unique(list_initiators$factions)),
      
      sliderInput("amends_connection",
                  "Connections",
                  min = 1, 
                  max = 15,
                  value = 5)),
    
    mainPanel(forceNetworkOutput("network_amends"),
              tableOutput("table_nodes"), 
              tableOutput("table_edges")
    )
  )
)


# server ####
server <- function(input, output, session) {
  
  # Download data
  list_initiators <- as.data.frame(list_initiators)
  edges_for_gephy <- as.data.frame(edges_for_gephy)
  
  # Nodes\vertices  
  nodes_rea <-reactive({
    
    nodes_reactive <- list_initiators%>%
      filter(factions %in% input$factions_input) 
  })
  
  # Edges\links   
  links_rea <-reactive({
    
    edges_reactive <- edges_for_gephy%>%
      filter(Value >= input$amends_connection) 
    
  })
  

# Render tables showing content of uploaded files 
output$table_edges <- renderTable({
  links_rea() # Edges
})

output$table_nodes <- renderTable({
  nodes_rea() #Nodes
})

output$network_amends <- renderForceNetwork({
  links1 <-links_rea()
  # These three lines have to solve problem with zero-indexing, but it doesn't work 
  # I still have this warning: It looks like Source/Target is not zero-indexed. This is required in JavaScript and so your plot may not render.
  links1$Source <- match(links_rea()$Source, nodes_rea()$ID_mps)-1
  links1$Target <- match(links_rea()$Target, nodes_rea()$ID_mps)-1
  
  forceNetwork(Links = links1, # source target   value
               Nodes = nodes_rea(), # name
               Source = "Source",
               Target = "Target", 
               Value = "Value",
               Group = "factions", # Colors
               NodeID = "names_mps",
               Nodesize = "weight_name",
               opacity = 1, 
               fontSize = 18, 
               zoom = T,  
               legend = TRUE,
               bounded = TRUE,
               charge=-10)
  
})


}

runApp(shinyApp(ui, server))

标签: javascriptrd3.jshtmlwidgetsnetworkd3

解决方案


过滤节点数据框 ( nodes_rea()) 后,您的链接数据框 ( links1/ links_rea()) 将具有链接/行,其中Source和/或Target值/s 在您的节点数据框中找不到,因此您的匹配命令重新索引节点在您的链接数据框中会产生一些NA值。过滤掉链接数据框中那些NA首先有 s 的行,它应该可以工作,即

links1 <- links1 %>% filter(!is.na(Source) & !is.na(Target))

推荐阅读