VisNetwork Problem integration with Shiny

Hi everybody

I'm new in the community, and normally i try to get the solution by myself, but in this case I didn't find any solution.
The problem is:
I start to use a library to build graphics and is amazing.
When I'm using it without shiny it works perfectly like in this code below:

base_nacional_malaria_final_graph = base_nacional_malaria_final %>% filter(ano_infec == 2017 ) %>% dplyr::select(cod_ibge_infeccao,cod_ibge_residencia,cod_ibge_notificacao,nome_municipio,ano_infec)

#Delete same municipality in the same row

deduped.new <- base_nacional_malaria_final_graph %>% filter(base_nacional_malaria_final_graph$cod_ibge_infeccao != base_nacional_malaria_final_graph$cod_ibge_residencia)
deduped.new  <-  deduped.new %>% filter( deduped.new$cod_ibge_residencia != deduped.new$cod_ibge_notificacao)
deduped.new  <-  deduped.new %>% filter( deduped.new$cod_ibge_infeccao != deduped.new$cod_ibge_notificacao & cod_ibge_residencia != "9999999" & cod_ibge_notificacao != "9999999" & cod_ibge_infeccao != "9999999" & nome_municipio != "9999999")

sources <- deduped.new %>%
  distinct(cod_ibge_residencia) %>%
  rename(label = cod_ibge_residencia)

destinations <- deduped.new %>%
  distinct(cod_ibge_infeccao) %>%
  rename(label = cod_ibge_infeccao)


nodes <- full_join(sources, destinations, by = "label")
nodes



nodes <- nodes %>% rowid_to_column("id")
nodes

municipio_brasil <- fread(input  = "data/Municipios_Brasileiros.csv",sep = ",",header = TRUE )
municipio_brasil <- municipio_brasil %>%
  rename(label = codigo)

nodes_new <- left_join(nodes, municipio_brasil, by = "label")
nodes_new
nodes_new <- nodes_new[c(-4:-8)]


per_route <- deduped.new %>%  
  group_by(cod_ibge_residencia, cod_ibge_infeccao, nome_municipio) %>%
  summarise(weight = n()) %>% 
  ungroup()
per_route


edges <- per_route %>% 
  left_join(nodes_new, by = c("cod_ibge_residencia" = "label")) %>% 
  rename(from = id)
edges
edges <- edges %>% 
  left_join(nodes_new, by = c("cod_ibge_infeccao" = "label")) %>% 
  rename(to = id)
edges
edges <- select(edges, from, to, weight)
edges

nodes_new_grap <- nodes_new[c(-2)]
colnames(nodes_new_grap)[colnames(nodes_new_grap)=="nome_municipio"] <- "label"
visNetwork(nodes_new_grap, edges)

edges <- mutate(edges, width = weight/3 + 1)
edges$title <- paste0("value : ", edges$weight)

visNetwork(nodes_new_grap, edges, main="Relationship between Municipality", submain="From Resident To Infection", footer = "Hyperlinks and mentions among media sources") %>% 
  visIgraphLayout(layout = "layout_with_fr") %>% 
  visEdges(arrows = "middle")%>% 
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) 


Then when i'm try to integrate in my shiny app there are some error coming up.
What itś wrong?

Shiny client UI:

# Tab Content (Faixa_etaria)
list_anos_faixa_network <- list(
  '2017'=2017,
  '2016'=2016,
  '2015'=2015,
  '2014'=2014,
  '2013'=2013,
  '2012'=2012,
  '2011'=2011,
  '2010'=2010,
  '2009'=2009,
  '2008'=2008,
  '2007'=2007,
  '2006'=2006,
  '2005'=2005,
  '2004'=2004,
  '2003'=2003
)

list_resexame_network <- list(
  'TOTAL'="TOTAL",
  'VIVAX'="VIVAX",
  'FALCIPARUM'="FALCIPARUM",
  'MISTA'="MISTA",
  'OVALE'="OVALE"
  
)

fluidRow(
  fluidRow(
    box(title = "Inputs", solidHeader = FALSE, width = 6,
        
        # Select variable for year
        selectInput(inputId = "ano_Network", 
                    label = "Year:",
                    choices = list_anos_faixa_pie),
        
        # Select variable for y-axis
        selectInput(inputId = "Network_input", 
                    label = "Resultado Exame:",
                    choices = list_resexame)
    ),
    #actionButton('tab_Network_button',label = 'Executar',icon=icon('table'))
    
    box(title = "Network Graphic", width = 6, solidHeader = FALSE, 
        
        visNetworkOutput("network"), 
        br(),
        verbatimTextOutput("shiny_return")))
)

Shiny Server:

output$network <- renderVisNetwork({
base_nacional_malaria_final_graph = base_nacional_malaria_final %>% filter(ano_infec == 2017 ) %>% dplyr::select(cod_ibge_infeccao,cod_ibge_residencia,cod_ibge_notificacao,nome_municipio,ano_infec)

#Delete same municipality in the same row

deduped.new <- base_nacional_malaria_final_graph %>% filter(base_nacional_malaria_final_graph$cod_ibge_infeccao != base_nacional_malaria_final_graph$cod_ibge_residencia)
deduped.new  <-  deduped.new %>% filter( deduped.new$cod_ibge_residencia != deduped.new$cod_ibge_notificacao)
deduped.new  <-  deduped.new %>% filter( deduped.new$cod_ibge_infeccao != deduped.new$cod_ibge_notificacao & cod_ibge_residencia != "9999999" & cod_ibge_notificacao != "9999999" & cod_ibge_infeccao != "9999999" & nome_municipio != "9999999")

sources <- deduped.new %>%
  distinct(cod_ibge_residencia) %>%
  rename(label = cod_ibge_residencia)

destinations <- deduped.new %>%
  distinct(cod_ibge_infeccao) %>%
  rename(label = cod_ibge_infeccao)


nodes <- full_join(sources, destinations, by = "label")
nodes



nodes <- nodes %>% rowid_to_column("id")
nodes

municipio_brasil <- fread(input  = "data/Municipios_Brasileiros.csv",sep = ",",header = TRUE )
municipio_brasil <- municipio_brasil %>%
  rename(label = codigo)

nodes_new <- left_join(nodes, municipio_brasil, by = "label")
nodes_new
nodes_new <- nodes_new[c(-4:-8)]


per_route <- deduped.new %>%  
  group_by(cod_ibge_residencia, cod_ibge_infeccao, nome_municipio) %>%
  summarise(weight = n()) %>% 
  ungroup()
per_route


edges <- per_route %>% 
  left_join(nodes_new, by = c("cod_ibge_residencia" = "label")) %>% 
  rename(from = id)
edges
edges <- edges %>% 
  left_join(nodes_new, by = c("cod_ibge_infeccao" = "label")) %>% 
  rename(to = id)
edges
edges <- select(edges, from, to, weight)
edges

nodes_new_grap <- nodes_new[c(-2)]
colnames(nodes_new_grap)[colnames(nodes_new_grap)=="nome_municipio"] <- "label"
#visNetwork(nodes_new_grap, edges)

edges <- mutate(edges, width = weight/3 + 1)
edges$title <- paste0("value : ", edges$weight)

# visNetwork(nodes_new_grap, edges, main="Relationship between Municipality", submain="From Resident To Infection", footer = "Hyperlinks and mentions among media sources") %>% 
#   visIgraphLayout(layout = "layout_with_fr") %>% 
#   visEdges(arrows = "middle")%>% 
#   visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) 



----------------------------------------------------------------
  
  

    visNetwork(nodes_new_grap, edges, main="Relationship between Municipality", submain="From Resident To Infection", footer = "Hyperlinks and mentions among media sources") %>%
      visIgraphLayout(layout = "layout_with_fr") %>% 
      visEdges(arrows = "middle")%>% 
      visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)#%>% 
       #visEvents(click = "function(nodes_new_grap){
        #           Shiny.onInputChange('click', nodes_new_grap.nodes_new_grap[0]);
         #          ;}"
       #)
  })

output$shiny_return <- renderPrint({
  visNetworkProxy("network") %>%
    visNearestNodes(target = input$click)
})

The error is : Warning: Error in -: invalid argument to unary operator

In the Server.
I would like to know why.
What itś wrong?

Thanks a lot
Alberto

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.