I am trying to build a shiny app with an interactive network, but I am stuck with two problems.
Firstly, I cannot figure out why I do have problem with zero indexing, because I used the solution to fix it, but it does not work properly. On the other hand, I do something wrong maybe.
Secondly, I have an issue with filtering the nodes based on the group (list_initiators$factions). Currently, the graph reacts when I want to change “connections”, but it has some malfunctions with factions. I want the graph to display only nodes from factions, which do have the connections. When I untick some factions in sidebox, the graph freezes when there is no connections and it doesn't react properly. You can see that in a table as well.
Here is my code
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))