Last active
October 9, 2022 23:55
-
-
Save gabrielzanlorenssi/10ea2e68ffa910f626049165983bb292 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bibliotecas ------------------------------------------------------------- | |
library(tidyverse) | |
library(sf) | |
library(jsonlite) | |
# Configuracao dos municipios --------------------------------------------- | |
urlm = "https://resultados.tse.jus.br/oficial/ele2022/544/config/mun-e000544-cm.json" | |
muni_tse <- fromJSON(urlm, | |
simplifyDataFrame = TRUE) %>% | |
.[['abr']] %>% | |
unnest('mu', names_repair="universal") %>% | |
select(-c, -z) %>% | |
set_names("uf", "estado", "tse", "ibge7", "nm") | |
# Dados ------------------------------------------------------------------- | |
url_muni = paste0("https://resultados.tse.jus.br/oficial/ele2022/544/dados/", str_to_lower(muni_tse$uf), | |
"/", str_to_lower(muni_tse$uf), muni_tse$tse, "-c0001-e000544-v.json") | |
# para evitar quedas do servidor do tse | |
rate <- rate_backoff(pause_base = 0.1, pause_min = 0.005, max_times = 100) | |
fs <- function(x,y) { | |
x<-fromJSON(x, simplifyDataFrame = T) | |
print(y) | |
return(x)} | |
insistent <- insistently(fs, | |
rate, | |
quiet = FALSE) | |
municipios <- imap(url_muni, insistent) | |
# juntar todos os dados | |
x <- municipios %>% | |
bind_rows() %>% | |
.[["abr"]] %>% | |
unnest(cand, names_repair="universal") | |
# tratamento dos dados | |
x %>% | |
filter(tpabr=="MU") %>% | |
left_join(muni_tse, by=c("cdabr"="tse")) %>% | |
select(cdabr, ibge7, nm, uf, e...20, c, a, vv, vn, vb, vap, n) %>% | |
pivot_wider(names_from="n", values_from="vap") %>% | |
select(cdabr:vb, | |
`13`, `22`, `15`, `12`, `14`, `16`, `21`, `27`, `30`, `44`, `80`) %>% | |
rename(tse =cdabr, municipio=nm, comparecimento=c, eleitores=e...20, abstencoes=a, | |
validos = vv, nulos = vn, brancos=vb) -> final |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment