#######################
##### Cid Edson #######
#######################
#library(devtools)
#install_github("vqv/ggbiplot")

library(readxl)
library(tidyverse)
library(visNetwork)
library(knitr)
library(networkD3)

opts_chunk$set(fig.path = "fig/script01")
options(width = 90)
options(knitr.table.format = "pandoc")
options(knitr.kable.NA = '')


setwd("E:/Google/UESC/R/Scritps/Edgar")
dados1 <- read_excel("dados2.xlsx", sheet = "01")
dados2 <- read_excel("dados2.xlsx", sheet = "02")
dados3 <- read_excel("dados2.xlsx", sheet = "03")
dados4 <- read_excel("dados2.xlsx", sheet = "04")

Dados da Tabela 1

sources <- dados1 %>%
  distinct(Potência) %>%
  rename(label = Potência)

destinations <- dados1 %>%
  distinct(`País em que possui lojas`) %>%
  rename(label = `País em que possui lojas`)


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


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


per_route <- dados1 %>%
  group_by(Potência, `País em que possui lojas`) %>%
  summarise(weight = n()) %>%
  ungroup()

#per_route %>% kable


edges <- per_route %>%
  left_join(nodes, by = c("Potência" = "label")) %>%
  rename(from = id)

edges <- edges %>%
  left_join(nodes, by = c(`País em que possui lojas` = "label")) %>%
  rename(to = id)

edges <- select(edges, from, to, weight)

Gráfico de Rede da Tabela 1

visNetwork(nodes, edges, height = "1000px", width = "100%")
edges <- mutate(edges, width = weight / 5 + 1)

visNetwork(nodes, edges,  height = "1200px", width = "100%") %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visEdges(arrows = "middle") %>%
  visEdges(smooth = TRUE)
nodes_d3 <- mutate(nodes, id = id - 1)
edges_d3 <- mutate(edges, from = from - 1, to = to - 1)

Sankey Diagram da Tabela 1

sankeyNetwork(
  Links = edges_d3,
  Nodes = nodes_d3,
  Source = "from",
  Target = "to",
  NodeID = "label",
  Value = "weight",
  fontSize = 16,
  unit = "Nó(s)"
)
## Links is a tbl_df. Converting to a plain data frame.
## Nodes is a tbl_df. Converting to a plain data frame.

Dados da Tabela 2

sources2 <- dados2 %>%
  distinct(`Potência Europeia Reconhecedora`) %>%
  rename(label = `Potência Europeia Reconhecedora`)

destinations2 <- dados2 %>%
  distinct(`Potência Reconhecida`) %>%
  rename(label = `Potência Reconhecida`)


nodes2 <- full_join(sources2, destinations2, by = "label")


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


per_route2 <- dados2 %>%
  group_by(`Potência Europeia Reconhecedora`,  `Potência Reconhecida`) %>%
  summarise(weight = n()) %>%
  ungroup()

#per_route2 %>% kable

edges2 <- per_route2 %>%
  left_join(nodes2, by = c("Potência Europeia Reconhecedora" = "label")) %>%
  rename(from = id)

edges2 <- edges2 %>%
  left_join(nodes2, by = c("Potência Reconhecida" = "label")) %>%
  rename(to = id)

edges2 <- select(edges2, from, to, weight)

Gráfico de Rede da Tabela 2

visNetwork(nodes2, edges2, height = "1000px", width = "100%")
edges2 <- mutate(edges2, width = weight / 5 + 1)

visNetwork(nodes2, edges2, height = "1000px", width = "100%") %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visEdges(arrows = "middle")  %>%
  visEdges(smooth = TRUE)
nodes_d4 <- mutate(nodes2, id = id - 1)
edges_d4 <- mutate(edges2, from = from - 1, to = to - 1)

Sankey Diagram da Tabela 2

sankeyNetwork(
  Links = edges_d4,
  Nodes = nodes_d4,
  Source = "from",
  Target = "to",
  NodeID = "label",
  Value = "weight",
  fontSize = 10,
  unit = "Nó(s)"
)
## Links is a tbl_df. Converting to a plain data frame.
## Nodes is a tbl_df. Converting to a plain data frame.

Dados da Tabela 3

sources3 <- dados3 %>%
  distinct(Potência) %>%
  rename(label = Potência)

destinations3 <- dados3 %>%
  distinct(País) %>%
  rename(label = País)


nodes3 <- full_join(sources3, destinations3, by = "label")


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


per_route3 <- dados3 %>%
  group_by(Potência, País) %>%
  summarise(weight = n()) %>%
  ungroup()

#per_route3 %>% kable


edges3 <- per_route3 %>%
  left_join(nodes3, by = c("Potência" = "label")) %>%
  rename(from = id)

edges3 <- edges3 %>%
  left_join(nodes3, by = c("País" = "label")) %>%
  rename(to = id)

edges3 <- select(edges3, from, to, weight)

Gráfico de Rede da Tabela 3

visNetwork(nodes3, edges3, height = "1000px", width = "100%")
edges3 <- mutate(edges3, width = weight / 5 + 1)

visNetwork(nodes3, edges3,  height = "1200px", width = "100%") %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visEdges(arrows = "middle") %>%
  visEdges(smooth = TRUE)
nodes_d5 <- mutate(nodes3, id = id - 1)
edges_d5 <- mutate(edges3, from = from - 1, to = to - 1)

Sankey Diagram da Tabela 3

sankeyNetwork(
  Links = edges_d5,
  Nodes = nodes_d5,
  Source = "from",
  Target = "to",
  NodeID = "label",
  Value = "weight",
  fontSize = 16,
  unit = "Nó(s)"
)
## Links is a tbl_df. Converting to a plain data frame.
## Nodes is a tbl_df. Converting to a plain data frame.

Dados da Tabela 4

sources4 <- dados4 %>%
  distinct(Potência) %>%
  rename(label = Potência)

destinations4 <- dados4 %>%
  distinct(`País de Origem` ) %>%
  rename(label = `País de Origem` )


nodes4 <- full_join(sources4, destinations4, by = "label")


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


per_route4 <- dados4 %>%
  group_by(Potência, `País de Origem` ) %>%
  summarise(weight = n()) %>%
  ungroup()

#per_route4 %>% kable


edges4 <- per_route4 %>%
  left_join(nodes4, by = c("Potência" = "label")) %>%
  rename(from = id)

edges4 <- edges4 %>%
  left_join(nodes4, by = c(`País de Origem` = "label")) %>%
  rename(to = id)

edges4 <- select(edges4, from, to, weight)

Gráfico de Rede da Tabela 4

visNetwork(nodes4, edges4, height = "1000px", width = "100%")
edges4 <- mutate(edges4, width = weight / 5 + 1)

visNetwork(nodes4, edges4,  height = "1200px", width = "100%") %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visEdges(arrows = "middle") %>%
  visEdges(smooth = TRUE)
nodes_d6 <- mutate(nodes4, id = id - 1)
edges_d6 <- mutate(edges4, from = from - 1, to = to - 1)

Sankey Diagram da Tabela 4

sankeyNetwork(
  Links = edges_d6,
  Nodes = nodes_d6,
  Source = "from",
  Target = "to",
  NodeID = "label",
  Value = "weight",
  fontSize = 16,
  unit = "Nó(s)"
)
## Links is a tbl_df. Converting to a plain data frame.
## Nodes is a tbl_df. Converting to a plain data frame.
library(collapsibleTree)
d1 <- data.frame(dados1)
d2 <- data.frame(dados2)
d3 <- data.frame(dados3)
d4 <- data.frame(dados4)
collapsibleTree(d1, c("Potência", "País.em.que.possui.lojas"))
collapsibleTree(d2, c("Potência.Europeia.Reconhecedora", "Potência.Reconhecida"))
collapsibleTree(d3, c("País", "Potência"))
collapsibleTree(d4, c("País.de.Origem", "Potência"))