Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandobarbalho/08232afd721f1ef382b1f29777f1c1c6 to your computer and use it in GitHub Desktop.
Save fernandobarbalho/08232afd721f1ef382b1f29777f1c1c6 to your computer and use it in GitHub Desktop.
# install.packages("devtools")
# devtools::install_github("tchiluanda/rsiconfi")
#Criar gráfico de sankey
df_desp <- get_dca(2018,"I-D","1")
df_desp_trabalho<-
df_desp %>%
#filter(stringr::str_starts(cod_conta,"R")) %>%
filter(coluna == "Despesas Liquidadas") %>%
mutate(nivel_0 = cod_conta =="TotalDespesas" ) %>%
mutate(nivel_1= (stringr::str_ends(cod_conta,"0.00.00.00.00"))) %>%
mutate(nivel_2= ((stringr::str_sub(cod_conta,5,5)!= "0") & (stringr::str_ends(cod_conta,"00.00.00.00")))) %>%
mutate(nivel_3= ((stringr::str_sub(cod_conta,7,8)!= "00") & (stringr::str_ends(cod_conta,"00.00.00")))) %>%
filter(nivel_0 | nivel_1 | nivel_2 | nivel_3 ) %>% #
mutate(pai = case_when(
nivel_1 ~ "TotalDespesas",
nivel_2 ~ paste0(stringr::str_sub(cod_conta,1,3), ".0.00.00.00.00"),
nivel_3 ~ paste0(stringr::str_sub(cod_conta,1,5), ".00.00.00.00")
)) %>%
filter(str_sub(cod_conta,1,2)!="DI")%>%
mutate (source = row_number() -1)
v_pai <-
df_desp_trabalho %>%
mutate(pai = ifelse(is.na(pai),cod_conta,pai))
pos_pai<-
map_int(v_pai$pai, function(a_pai){
print(a_pai)
which(df_desp_trabalho$cod_conta==a_pai)
})
pos_pai<- pos_pai -1
df_desp_trabalho$destination <- pos_pai
nodes<- df_desp_trabalho %>%
mutate(conta= case_when(
cod_conta == "TotalDespesas" ~ "Total de despesas liquidadas",
input$ano_desp == "2018" ~ stringr::str_sub(conta, 16,str_length(conta)),
input$ano_desp < "2018" ~ stringr::str_sub(conta, 18,str_length(conta))
)) %>%
select(conta)
links<-
df_desp_trabalho %>%
filter(!is.na(pai)) %>%
select(source,
destination,
valor)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "destination", Value = "valor", NodeID = "conta",
units = "", fontSize = 12, nodeWidth = 30)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment