【问题标题】:Zero-index problem and filtering issue in shiny闪亮中的零索引问题和过滤问题
【发布时间】:2020-09-20 19:07:25
【问题描述】:

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

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

其次,我在根据组 (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))

【问题讨论】:

  • list_initiators &lt;- read.csv(...) 在我的计算机上返回一个空数据集
  • 可能是编码问题:我没有看到链接中的字符
  • 这是我看到的:“��������������������","��","",5
  • fileEncoding = "Windows-1251" , read.csv() 中也提到过

标签: javascript r d3.js htmlwidgets networkd3


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多