From 37a8e81c1be3fcb0603a5452c8d42889df6eec6e Mon Sep 17 00:00:00 2001 From: Andre Felix Date: Mon, 29 Jun 2020 11:44:01 -0300 Subject: [PATCH 1/6] Centraliza funcao de download. Ref #6 --- R/candidate_fed.R | 7 +------ R/candidate_local.R | 9 ++------- R/details_mun_zone_fed.R | 7 +------ R/details_mun_zone_local.R | 8 +------- R/legend_fed.R | 17 +++++------------ R/legend_local.R | 9 ++------- R/party_mun_zone_fed.R | 9 ++------- R/party_mun_zone_local.R | 8 +------- R/personal_finances_fed.R | 13 +++---------- R/personal_finances_local.R | 9 ++------- R/seats_fed.R | 13 +++---------- R/seats_local.R | 9 ++------- R/utils.R | 8 ++++++++ R/vote_mun_zone_fed.R | 9 ++------- R/vote_mun_zone_local.R | 7 +------ R/vote_section_fed.R | 7 +------ R/vote_section_local.R | 29 ++++++++--------------------- R/voter_profile.R | 8 +------- 18 files changed, 46 insertions(+), 140 deletions(-) diff --git a/R/candidate_fed.R b/R/candidate_fed.R index 9e3c9e0..a34b72c 100644 --- a/R/candidate_fed.R +++ b/R/candidate_fed.R @@ -121,12 +121,7 @@ candidate_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, e uf <- test_uf(uf) test_br(br_archive) - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf('odsele/consulta_cand/consulta_cand_%s.zip', year), year) message("Processing the data...") diff --git a/R/candidate_local.R b/R/candidate_local.R index 01ae4d9..f84cedf 100644 --- a/R/candidate_local.R +++ b/R/candidate_local.R @@ -92,13 +92,8 @@ candidate_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1" test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - - # Downloads the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + + download_and_unzip_datafile(sprintf("odsele/consulta_cand/consulta_cand_%s.zip", year), year) message("Processing the data...") diff --git a/R/details_mun_zone_fed.R b/R/details_mun_zone_fed.R index 22fd73b..6ee1beb 100644 --- a/R/details_mun_zone_fed.R +++ b/R/details_mun_zone_fed.R @@ -91,12 +91,7 @@ details_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = F uf <- test_uf(uf) test_br(br_archive) - # Downloads the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf("odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%s.zip", year), year) message("Processing the data...") diff --git a/R/details_mun_zone_local.R b/R/details_mun_zone_local.R index a27a715..2758b35 100644 --- a/R/details_mun_zone_local.R +++ b/R/details_mun_zone_local.R @@ -68,18 +68,12 @@ details_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ - # Input tests test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - # Downloads the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf("odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%s.zip", year), year) message("Processing the data...") diff --git a/R/legend_fed.R b/R/legend_fed.R index 7b28137..c2f2987 100644 --- a/R/legend_fed.R +++ b/R/legend_fed.R @@ -70,20 +70,14 @@ legend_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - - + if(year < 2018) { - endereco <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_legendas/consulta_legendas_%s.zip" + endereco <- "odsele/consulta_legendas/consulta_legendas_%s.zip" } else{ - endereco <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_coligacao/consulta_coligacao_%s.zip" + endereco <- "odsele/consulta_coligacao/consulta_coligacao_%s.zip" } - # Download the data - dados <- tempfile() - sprintf(endereco, year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf(endereco, year), year) message("Processing the data...") @@ -106,8 +100,7 @@ legend_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_COLIGACAO", "NOME_COLIGACAO", "COMPOSICAO_COLIGACAO") } - - + # Change to ascii if(ascii == T) banco <- to_ascii(banco, encoding) diff --git a/R/legend_local.R b/R/legend_local.R index 5d273ec..e34051f 100644 --- a/R/legend_local.R +++ b/R/legend_local.R @@ -65,13 +65,8 @@ legend_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", e uf <- test_uf(uf) if(year > 2004){ - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_legendas/consulta_legendas_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - + download_and_unzip_datafile(sprintf("odsele/consulta_legendas/consulta_legendas_%s.zip", year), year) + message("Processing the data...") # Cleans the data diff --git a/R/party_mun_zone_fed.R b/R/party_mun_zone_fed.R index a6082af..6108646 100644 --- a/R/party_mun_zone_fed.R +++ b/R/party_mun_zone_fed.R @@ -84,15 +84,10 @@ party_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FA uf <- test_uf(uf) test_br(br_archive) - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_partido_munzona/votacao_partido_munzona_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf("odsele/votacao_partido_munzona/votacao_partido_munzona_%s.zip", year), year) message("Processing the data...") - + # Cleans the data setwd(as.character(year)) banco <- juntaDados(uf, encoding, br_archive) diff --git a/R/party_mun_zone_local.R b/R/party_mun_zone_local.R index 831fd9b..816f4c0 100644 --- a/R/party_mun_zone_local.R +++ b/R/party_mun_zone_local.R @@ -70,16 +70,10 @@ party_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "la test_local_year(year) uf <- test_uf(uf) - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_partido_munzona/votacao_partido_munzona_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf("odsele/votacao_partido_munzona/votacao_partido_munzona_%s.zip", year), year) message("Processing the data...") - # Cleans the data setwd(as.character(year)) banco <- juntaDados(uf, encoding, FALSE) diff --git a/R/personal_finances_fed.R b/R/personal_finances_fed.R index d998b09..7753976 100644 --- a/R/personal_finances_fed.R +++ b/R/personal_finances_fed.R @@ -72,13 +72,8 @@ personal_finances_fed <- function(year, uf = "all", br_archive = FALSE, ascii = if(year < 2006) stop("Not disponible. Please, check the documentation and try again.\n") - # Downloads the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/bem_candidato/bem_candidato_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - + download_and_unzip_datafile(sprintf("odsele/bem_candidato/bem_candidato_%s.zip", year), year) + message("Processing the data...") # Cleans the data @@ -109,6 +104,4 @@ personal_finances_fed <- function(year, uf = "all", br_archive = FALSE, ascii = message("Done.\n") return(banco) -} - - \ No newline at end of file +} diff --git a/R/personal_finances_local.R b/R/personal_finances_local.R index cdc5f50..54298c8 100644 --- a/R/personal_finances_local.R +++ b/R/personal_finances_local.R @@ -54,13 +54,8 @@ personal_finances_local <- function(year, uf = "all", ascii = FALSE, encoding = test_local_year(year) uf <- test_uf(uf) - # Downloads the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/bem_candidato/bem_candidato_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - + download_and_unzip_datafile(sprintf("odsele/bem_candidato/bem_candidato_%s.zip", year), year) + message("Processing the data...") # Cleans the data diff --git a/R/seats_fed.R b/R/seats_fed.R index 3cbc22b..d505e51 100644 --- a/R/seats_fed.R +++ b/R/seats_fed.R @@ -61,14 +61,9 @@ seats_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_vagas/consulta_vagas_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - + + download_and_unzip_datafile(sprintf("odsele/consulta_vagas/consulta_vagas_%s.zip", year), year) + message("Processing the data...") # Cleans the data @@ -89,7 +84,6 @@ seats_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco "CODIGO_CARGO", "DESCRICAO_CARGO", "QTDE_VAGAS" ) } - # Change to ascii if(ascii) banco <- to_ascii(banco, encoding) @@ -99,4 +93,3 @@ seats_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco message("Done.\n") return(banco) } - diff --git a/R/seats_local.R b/R/seats_local.R index 308f41b..2133732 100644 --- a/R/seats_local.R +++ b/R/seats_local.R @@ -56,13 +56,8 @@ seats_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", ex test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_vagas/consulta_vagas_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + + download_and_unzip_datafile(sprintf("odsele/consulta_vagas/consulta_vagas_%s.zip", year), year) message("Processing the data...") diff --git a/R/utils.R b/R/utils.R index 4d97f22..6993e3e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -159,6 +159,14 @@ export_data <- function(df) { message(paste0("Electoral data files were saved on: ", getwd(), ".\n")) } +download_and_unzip_datafile <- function(str_endpoint, year) { + str_base_url <- 'http://agencia.tse.jus.br/estatistica/sead/%s' + + tmp_data_file <- tempfile() + download.file(sprintf(str_base_url, str_endpoint), tmp_data_file) + unzip(tmp_data_file, exdir = paste0("./", year)) + unlink(tmp_data_file) +} # Avoid the R CMD check note about magrittr's dot utils::globalVariables(".") diff --git a/R/vote_mun_zone_fed.R b/R/vote_mun_zone_fed.R index e02b8c1..f48b993 100644 --- a/R/vote_mun_zone_fed.R +++ b/R/vote_mun_zone_fed.R @@ -88,13 +88,8 @@ vote_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FAL test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + + download_and_unzip_datafile(sprintf("odsele/votacao_candidato_munzona/votacao_candidato_munzona_%s.zip", year), year) message("Processing the data...") diff --git a/R/vote_mun_zone_local.R b/R/vote_mun_zone_local.R index c40658f..5e2eb04 100644 --- a/R/vote_mun_zone_local.R +++ b/R/vote_mun_zone_local.R @@ -72,12 +72,7 @@ vote_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "la test_local_year(year) uf <- test_uf(uf) - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf("odsele/votacao_candidato_munzona/votacao_candidato_munzona_%s.zip", year), year) message("Processing the data...") diff --git a/R/vote_section_fed.R b/R/vote_section_fed.R index 68c6545..e57bf53 100644 --- a/R/vote_section_fed.R +++ b/R/vote_section_fed.R @@ -64,12 +64,7 @@ vote_section_fed <- function(year, uf = "AC", ascii = FALSE, encoding = "latin1" if(tolower(uf) == "all") stop("'uf' is invalid. Please, check the documentation and try again.") uf <- test_uf(uf) - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_secao/votacao_secao_%s_%s.zip", year, uf) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + download_and_unzip_datafile(sprintf("odsele/votacao_secao/votacao_secao_%s_%s.zip", year, uf), year) message("Processing the data...") diff --git a/R/vote_section_local.R b/R/vote_section_local.R index 78f33d2..f398a52 100644 --- a/R/vote_section_local.R +++ b/R/vote_section_local.R @@ -66,13 +66,8 @@ vote_section_local <- function(year, uf = "AC", ascii = FALSE, encoding = "latin if(year < 2012){ - # Download the data - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_secao/votacao_secao_%s_%s.zip", year, uf) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - + download_and_unzip_datafile(sprintf("odsele/votacao_secao/votacao_secao_%s_%s.zip", year, uf), year) + message("Processing the data...") # Clean the data @@ -84,12 +79,8 @@ vote_section_local <- function(year, uf = "AC", ascii = FALSE, encoding = "latin } else{ message("Download the data One...") - dados <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/eleicoes/eleicoes2012/votosecao/vsec_1t_%s.zip", uf) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - + download_and_unzip_datafile(sprintf("/eleicoes/eleicoes2012/votosecao/vsec_1t_%s.zip", uf), year) + message("Processing the data one...") # Clean the data @@ -100,15 +91,11 @@ vote_section_local <- function(year, uf = "AC", ascii = FALSE, encoding = "latin if(!(uf %in% c("AL", "DF", "GO", "PE", "RR", "SE", "TO"))){ - + message("Download the data two...") - - dados2 <- tempfile() - sprintf("http://agencia.tse.jus.br/estatistica/sead/eleicoes/eleicoes2012/votosecao/vsec_2t_%s_30102012194527.zip", uf) %>% - download.file(dados2) - unzip(dados2, exdir = paste0("./", year, "2")) - unlink(dados2) - + + download_and_unzip_datafile(sprintf("eleicoes/eleicoes2012/votosecao/vsec_2t_%s_30102012194527.zip", uf), year) + message("Processing the data two...") # Clean the data diff --git a/R/voter_profile.R b/R/voter_profile.R index a3282a1..da9283e 100644 --- a/R/voter_profile.R +++ b/R/voter_profile.R @@ -45,14 +45,8 @@ voter_profile <- function(year, ascii = FALSE, encoding = "windows-1252", export if(!year %in% seq(1994, 2018, by = 2)) stop("Invalid 'year'. Please check the documentation and try again.") test_encoding(encoding) - # Download data - dados <- tempfile() + download_and_unzip_datafile(sprintf("odsele/perfil_eleitorado/perfil_eleitorado_%s.zip", year), year) - sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/perfil_eleitorado/perfil_eleitorado_%s.zip", year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - # Join data message("Processing the data...") From 6b1b18080d2cc671549025d245d7984ffd795334 Mon Sep 17 00:00:00 2001 From: Andre Felix Date: Wed, 1 Jul 2020 07:15:24 -0300 Subject: [PATCH 2/6] Centraliza tratamento dos dados. Ref #6 --- R/candidate_fed.R | 29 +++------------ R/candidate_local.R | 31 +++------------- R/details_mun_zone_fed.R | 28 +++----------- R/details_mun_zone_local.R | 26 ++----------- R/legend_fed.R | 30 +++------------ R/legend_local.R | 38 +++++-------------- R/party_mun_zone_fed.R | 32 ++++------------ R/party_mun_zone_local.R | 27 ++------------ R/personal_finances_fed.R | 52 +++++++++----------------- R/personal_finances_local.R | 32 +++------------- R/seats_fed.R | 28 +++----------- R/seats_local.R | 31 +++------------- R/utils.R | 53 ++++++++++++++++++++++++++- R/vote_mun_zone_fed.R | 31 +++------------- R/vote_mun_zone_local.R | 28 +++----------- R/vote_section_fed.R | 35 +++++------------- R/vote_section_local.R | 73 +++++++++---------------------------- 17 files changed, 170 insertions(+), 434 deletions(-) diff --git a/R/candidate_fed.R b/R/candidate_fed.R index a34b72c..bebe428 100644 --- a/R/candidate_fed.R +++ b/R/candidate_fed.R @@ -114,26 +114,15 @@ candidate_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ - # Input tests test_encoding(encoding) test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - download_and_unzip_datafile(sprintf('odsele/consulta_cand/consulta_cand_%s.zip', year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names + # Variable names if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "NOME_CANDIDATO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "CPF_CANDIDATO", "NOME_URNA_CANDIDATO", "COD_SITUACAO_CANDIDATURA", "DES_SITUACAO_CANDIDATURA", @@ -147,7 +136,7 @@ candidate_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, e "DESC_SIT_TOT_TURNO") }else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO", @@ -163,13 +152,7 @@ candidate_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, e "SITUACAO_REELEICAO", "SITUACAO_DECLARAR_BENS", "NUMERO_PROTOCOLO_CANDIDATURA", "NUMERO_PROCESSO") } - - # Change to ascii - if(ascii) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + + get_data('consulta_cand', year, uf, br_archive, ascii, encoding, export, data_names) } + diff --git a/R/candidate_local.R b/R/candidate_local.R index f84cedf..41368b7 100644 --- a/R/candidate_local.R +++ b/R/candidate_local.R @@ -93,19 +93,9 @@ candidate_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1" test_local_year(year) uf <- test_uf(uf) - download_and_unzip_datafile(sprintf("odsele/consulta_cand/consulta_cand_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - # Changes variables names if(year < 2012){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "NOME_CANDIDATO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "CPF_CANDIDATO", "NOME_URNA_CANDIDATO", "COD_SITUACAO_CANDIDATURA", "DES_SITUACAO_CANDIDATURA", @@ -117,9 +107,8 @@ candidate_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1" "DESCRICAO_NACIONALIDADE", "SIGLA_UF_NASCIMENTO", "CODIGO_MUNICIPIO_NASCIMENTO", "NOME_MUNICIPIO_NASCIMENTO", "DESPESA_MAX_CAMPANHA", "COD_SIT_TOT_TURNO", "DESC_SIT_TOT_TURNO") - } else if(year == 2012) { - names(banco) <-c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <-c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "NOME_CANDIDATO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "CPF_CANDIDATO", "NOME_URNA_CANDIDATO", "COD_SITUACAO_CANDIDATURA", "DES_SITUACAO_CANDIDATURA", @@ -131,10 +120,8 @@ candidate_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1" "DESCRICAO_NACIONALIDADE", "SIGLA_UF_NASCIMENTO", "CODIGO_MUNICIPIO_NASCIMENTO", "NOME_MUNICIPIO_NASCIMENTO", "DESPESA_MAX_CAMPANHA", "COD_SIT_TOT_TURNO", "DESC_SIT_TOT_TURNO", "EMAIL_CANDIDATO") - } else { - - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO", @@ -149,15 +136,7 @@ candidate_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1" "DESCRICAO_OCUPACAO", "DESPESA_MAX_CAMPANHA", "COD_SIT_TOT_TURNO", "DESC_SIT_TOT_TURNO", "SITUACAO_REELEICAO", "SITUACAO_DECLARAR_BENS", "NUMERO_PROTOCOLO_CANDIDATURA", "NUMERO_PROCESSO") - } - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + + get_data('consulta_cand', year, uf, FALSE, ascii, encoding, export, data_names) } diff --git a/R/details_mun_zone_fed.R b/R/details_mun_zone_fed.R index 6ee1beb..ae6a10c 100644 --- a/R/details_mun_zone_fed.R +++ b/R/details_mun_zone_fed.R @@ -84,26 +84,15 @@ details_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ - # Input tests test_encoding(encoding) test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - download_and_unzip_datafile(sprintf("odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - - # Changes variables names + # Variables names if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTD_APTOS", "QTD_SECOES", "QTD_SECOES_AGREGADAS", "QTD_APTOS_TOT", "QTD_SECOES_TOT", "QTD_COMPARECIMENTO", "QTD_ABSTENCOES", @@ -111,7 +100,7 @@ details_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = F "QTD_VOTOS_ANULADOS_APU_SEP", "DATA_ULT_TOTALIZACAO", "HORA_ULT_TOTALIZACAO") } else if( year == 2014 ) { - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTD_APTOS", "QTD_SECOES", "QTD_SECOES_AGREGADAS", "QTD_APTOS_TOT", "QTD_SECOES_TOT", "QTD_COMPARECIMENTO", "QTD_ABSTENCOES", @@ -120,7 +109,7 @@ details_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = F "VAR_NAO_DECLARADA_DOC_TSE") } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTD_APTOS", "QTD_SECOES", "QTD_SECOES_AGREGADAS", "QTD_APTOS_TOT", @@ -128,13 +117,6 @@ details_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = F "QTD_VOTOS_BRANCOS", "QTD_VOTOS_NULOS", "QTD_VOTOS_LEGENDA", "QTD_VOTOS_PENDENTES", "QTD_VOTOS_ANULADOS", "HORA_ULT_TOTALIZACAO", "DATA_ULT_TOTALIZACAO") } - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - message("Done.\n") - return(banco) + get_data('detalhe_votacao_munzona', year, uf, br_archive, ascii, encoding, export, data_names) } diff --git a/R/details_mun_zone_local.R b/R/details_mun_zone_local.R index 2758b35..30ed731 100644 --- a/R/details_mun_zone_local.R +++ b/R/details_mun_zone_local.R @@ -73,19 +73,9 @@ details_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = " test_local_year(year) uf <- test_uf(uf) - download_and_unzip_datafile(sprintf("odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - - # Changes variables names + # Variables names if(year <= 2012){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTD_APTOS", "QTD_SECOES", "QTD_SECOES_AGREGADAS", "QTD_APTOS_TOT", "QTD_SECOES_TOT", "QTD_COMPARECIMENTO", "QTD_ABSTENCOES", @@ -93,8 +83,7 @@ details_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = " "QTD_VOTOS_ANULADOS_APU_SEP", "DATA_ULT_TOTALIZACAO", "HORA_ULT_TOTALIZACAO") } else { - - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTD_APTOS", "QTD_SECOES", "QTD_SECOES_AGREGADAS", "QTD_APTOS_TOT", @@ -102,13 +91,6 @@ details_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = " "QTD_VOTOS_BRANCOS", "QTD_VOTOS_NULOS", "QTD_VOTOS_LEGENDA", "QTD_VOTOS_PENDENTES", "QTD_VOTOS_ANULADOS", "HORA_ULT_TOTALIZACAO", "DATA_ULT_TOTALIZACAO") } - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - message("Done.\n") - return(banco) + get_data("detalhe_votacao_munzona", year, uf, FALSE, ascii, encoding, export, data_names) } diff --git a/R/legend_fed.R b/R/legend_fed.R index c2f2987..24ac117 100644 --- a/R/legend_fed.R +++ b/R/legend_fed.R @@ -64,7 +64,6 @@ legend_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ - # Test the input test_encoding(encoding) test_fed_year(year) @@ -72,41 +71,24 @@ legend_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco test_br(br_archive) if(year < 2018) { - endereco <- "odsele/consulta_legendas/consulta_legendas_%s.zip" + data_name <- "consulta_legendas" } else{ - endereco <- "odsele/consulta_coligacao/consulta_coligacao_%s.zip" + data_name <- "consulta_coligacao" } - download_and_unzip_datafile(sprintf(endereco, year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names + # Variable names if(year < 2018) { - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "NOME_MUNICIPIO", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SIGLA_COLIGACAO", "NOME_COLIGACAO", "COMPOSICAO_COLIGACAO", "SEQUENCIAL_COLIGACAO") } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NM_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NM_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "SIGLA_UF", "SIGLA_UE", "NOME_MUNICIPIO", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_COLIGACAO", "NOME_COLIGACAO", "COMPOSICAO_COLIGACAO") } - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + get_data(data_name, year, uf, br_archive, ascii, encoding, export, data_names) } diff --git a/R/legend_local.R b/R/legend_local.R index e34051f..af67063 100644 --- a/R/legend_local.R +++ b/R/legend_local.R @@ -58,40 +58,20 @@ legend_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ + if(year <= 2004){ + stop("Not disponible. Please, check the documentation and try again.\n") + } # Test the input test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - if(year > 2004){ - download_and_unzip_datafile(sprintf("odsele/consulta_legendas/consulta_legendas_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", - "SIGLA_UF", "SIGLA_UE", "NOME_MUNICIPIO", "CODIGO_CARGO", "DESCRICAO_CARGO", - "TIPO_LEGENDA", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SIGLA_COLIGACAO", - "NOME_COLIGACAO", "COMPOSICAO_COLIGACAO", "SEQUENCIAL_COLIGACAO") - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) - }else{ - message("Not disponible. Please, check the documentation and try again.\n") - } + # Variable names + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + "SIGLA_UF", "SIGLA_UE", "NOME_MUNICIPIO", "CODIGO_CARGO", "DESCRICAO_CARGO", + "TIPO_LEGENDA", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SIGLA_COLIGACAO", + "NOME_COLIGACAO", "COMPOSICAO_COLIGACAO", "SEQUENCIAL_COLIGACAO") - + get_data('consulta_legendas', year, uf, FALSE, ascii, encoding, export, data_names) } diff --git a/R/party_mun_zone_fed.R b/R/party_mun_zone_fed.R index 6108646..19b8473 100644 --- a/R/party_mun_zone_fed.R +++ b/R/party_mun_zone_fed.R @@ -75,8 +75,7 @@ #' df <- party_mun_zone_fed(2002) #' } -party_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ - +party_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ # Test the input test_encoding(encoding) @@ -84,45 +83,28 @@ party_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FA uf <- test_uf(uf) test_br(br_archive) - download_and_unzip_datafile(sprintf("odsele/votacao_partido_munzona/votacao_partido_munzona_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names + # Variable names if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "SIGLA_PARTIDO", "NUMERO_PARTIDO", "NOME_PARTIDO", "QTDE_VOTOS_NOMINAIS", "QTDE_VOTOS_LEGENDA", "SEQUENCIAL_LEGENDA") } else if(year == 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "SIGLA_PARTIDO", "NUMERO_PARTIDO", "NOME_PARTIDO", "QTDE_VOTOS_NOMINAIS", "QTDE_VOTOS_LEGENDA", "TRANSITO", "SEQUENCIAL_LEGENDA") } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "TRANSITO", "QTDE_VOTOS_NOMINAIS", "QTDE_VOTOS_LEGENDA") } - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + + get_data('votacao_partido_munzona', year, uf, br_archive, ascii, encoding, export, data_names) } diff --git a/R/party_mun_zone_local.R b/R/party_mun_zone_local.R index 816f4c0..91f1a89 100644 --- a/R/party_mun_zone_local.R +++ b/R/party_mun_zone_local.R @@ -64,45 +64,26 @@ party_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ - # Test the input test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - download_and_unzip_datafile(sprintf("odsele/votacao_partido_munzona/votacao_partido_munzona_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names + # Variable names if(year <= 2012){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "SIGLA_PARTIDO", "NUMERO_PARTIDO", "NOME_PARTIDO", "QTDE_VOTOS_NOMINAIS", "QTDE_VOTOS_LEGENDA", "SEQUENCIAL_LEGENDA") } else { - - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "DESCRICAO_CARGO", "TIPO_LEGENDA", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "TRANSITO", "QTDE_VOTOS_NOMINAIS", "QTDE_VOTOS_LEGENDA") } - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + get_data('votacao_partido_munzona', year, uf, FALSE, ascii, encoding, export, data_names) } diff --git a/R/personal_finances_fed.R b/R/personal_finances_fed.R index 7753976..4015b36 100644 --- a/R/personal_finances_fed.R +++ b/R/personal_finances_fed.R @@ -62,46 +62,30 @@ #' df <- personal_finances_fed(2006) #' } -personal_finances_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ +personal_finances_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE) { + if(year < 2006){ + stop("Not disponible. Please, check the documentation and try again.\n") + } + # Input tests test_encoding(encoding) test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - if(year < 2006) stop("Not disponible. Please, check the documentation and try again.\n") - - download_and_unzip_datafile(sprintf("odsele/bem_candidato/bem_candidato_%s.zip", year), year) + # Variables names + if(year < 2014){ + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", + "SIGLA_UF", "SQ_CANDIDATO", "CD_TIPO_BEM_CANDIDATO", "DS_TIPO_BEM_CANDIDATO", + "DETALHE_BEM", "VALOR_BEM", "DATA_ULT_TOTALIZACAO", "HORA_ULT_TOTALIZACAO") + } else{ + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "SIGLA_UF", "SIGLA_UE", + "NOME_UE", "SQ_CANDIDATO", "NUMERO_ORDEM_CANDIDATO", "CD_TIPO_BEM_CANDIDATO", + "DS_TIPO_BEM_CANDIDATO", "DETALHE_BEM", "VALOR_BEM", "DT_ULTIMA_ATUALIZACAO", + "HH_ULTIMA_ATUALIZACAO") + } - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - - # Changes variables names - if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", - "SIGLA_UF", "SQ_CANDIDATO", "CD_TIPO_BEM_CANDIDATO", "DS_TIPO_BEM_CANDIDATO", - "DETALHE_BEM", "VALOR_BEM", "DATA_ULT_TOTALIZACAO", "HORA_ULT_TOTALIZACAO") - } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", - "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "SIGLA_UF", "SIGLA_UE", - "NOME_UE", "SQ_CANDIDATO", "NUMERO_ORDEM_CANDIDATO", "CD_TIPO_BEM_CANDIDATO", - "DS_TIPO_BEM_CANDIDATO", "DETALHE_BEM", "VALOR_BEM", "DT_ULTIMA_ATUALIZACAO", - "HH_ULTIMA_ATUALIZACAO") - } - - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + get_data('bem_candidato', year, uf, br_archive, ascii, encoding, export, data_names) } diff --git a/R/personal_finances_local.R b/R/personal_finances_local.R index 54298c8..eae4be6 100644 --- a/R/personal_finances_local.R +++ b/R/personal_finances_local.R @@ -46,45 +46,25 @@ #' df <- personal_finances_local(2000) #' } -personal_finances_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ - +personal_finances_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ # Input tests test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - - download_and_unzip_datafile(sprintf("odsele/bem_candidato/bem_candidato_%s.zip", year), year) - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - - # Changes variables names + # Variables names if(year < 2012){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SQ_CANDIDATO", "CD_TIPO_BEM_CANDIDATO", "DS_TIPO_BEM_CANDIDATO", "DETALHE_BEM", "VALOR_BEM", "DATA_ULT_TOTALIZACAO", "HORA_ULT_TOTALIZACAO") } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "SQ_CANDIDATO", "NUMERO_ORDEM_CANDIDATO", "CD_TIPO_BEM_CANDIDATO", "DS_TIPO_BEM_CANDIDATO", "DETALHE_BEM", "VALOR_BEM", "DT_ULTIMA_ATUALIZACAO", "HH_ULTIMA_ATUALIZACAO") } - - - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + + get_data('bem_candidato', year, uf, FALSE, ascii, encoding, export, data_names) } \ No newline at end of file diff --git a/R/seats_fed.R b/R/seats_fed.R index d505e51..a521a5c 100644 --- a/R/seats_fed.R +++ b/R/seats_fed.R @@ -53,8 +53,7 @@ #' df <- seats_fed(2000) #' } -seats_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ - +seats_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ # Input tests test_encoding(encoding) @@ -62,34 +61,17 @@ seats_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, enco uf <- test_uf(uf) test_br(br_archive) - download_and_unzip_datafile(sprintf("odsele/consulta_vagas/consulta_vagas_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names + # Variable names if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTDE_VAGAS") }else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "DATA_POSSE", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTDE_VAGAS" ) } - # Change to ascii - if(ascii) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + get_data('consulta_vagas', year, uf, br_archive, ascii, encoding, export, data_names) } diff --git a/R/seats_local.R b/R/seats_local.R index 2133732..66f4aa7 100644 --- a/R/seats_local.R +++ b/R/seats_local.R @@ -51,41 +51,22 @@ seats_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ - # Input tests test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - - download_and_unzip_datafile(sprintf("odsele/consulta_vagas/consulta_vagas_%s.zip", year), year) - - message("Processing the data...") - - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) # Change variable names -if( year < 2016 ){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", + if (year < 2016) { + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTDE_VAGAS") - } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", + } else { + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "DATA_POSSE", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_CARGO", "DESCRICAO_CARGO", "QTDE_VAGAS" ) } - - # Change to ascii - if(ascii) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) -} + get_data('consulta_vagas', year, uf, FALSE, ascii, encoding, export, data_names) +} diff --git a/R/utils.R b/R/utils.R index 6993e3e..d8b2d91 100644 --- a/R/utils.R +++ b/R/utils.R @@ -159,6 +159,25 @@ export_data <- function(df) { message(paste0("Electoral data files were saved on: ", getwd(), ".\n")) } +get_file_remote_location <- function(str_file_name) { + lst_file <- list( + list("name" = "consulta_cand", "url" = "odsele/consulta_cand/consulta_cand_%year%.zip"), + list("name" = "votacao_candidato_munzona", "url" = "odsele/votacao_candidato_munzona/votacao_candidato_munzona_%year%.zip"), + list("name" = "consulta_vagas", "url" = "odsele/consulta_vagas/consulta_vagas_%year%.zip"), + list("name" = "consulta_legendas", "url" = "odsele/consulta_legendas/consulta_legendas_%year%.zip"), + list("name" = "consulta_coligacao", "url" = "odsele/consulta_coligacao/consulta_coligacao_%year%.zip"), + list("name" = "votacao_partido_munzona", "url" = "odsele/votacao_partido_munzona/votacao_partido_munzona_%year%.zip"), + list("name" = "detalhe_votacao_munzona", "url" = "odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%year%.zip"), + list("name" = "votacao_secao", "url" = "odsele/votacao_secao/votacao_secao_%year%_%uf%.zip"), + list("name" = "vsec_1t", "url" = "/eleicoes/eleicoes2012/votosecao/vsec_1t_%year%.zip"), + list("name" = "vsec_2t", "url" = "eleicoes/eleicoes2012/votosecao/vsec_2t_%uf%_30102012194527.zip"), + list("name" = "bem_candidato", "url" = "odsele/bem_candidato/bem_candidato_%year%.zip") + ) + df_file <- do.call(rbind, lapply(lst_file, data.frame)) + df_file$url <- as.character(df_file$url) + return(df_file[df_file$name == str_file_name, c('url')]) +} + download_and_unzip_datafile <- function(str_endpoint, year) { str_base_url <- 'http://agencia.tse.jus.br/estatistica/sead/%s' @@ -168,6 +187,38 @@ download_and_unzip_datafile <- function(str_endpoint, year) { unlink(tmp_data_file) } +get_data <- function(str_data_name, year, uf, br_archive, ascii, encoding, export, data_names = NULL) { + str_remote_file_location <- get_file_remote_location(str_data_name) + str_remote_file_location <- gsub(pattern = '%year%', replacement = year, x = str_remote_file_location) + str_remote_file_location <- gsub(pattern = '%uf%', replacement = uf, x = str_remote_file_location) + + download_and_unzip_datafile(str_remote_file_location, year) + + message("Processing the data...") + + br_archive <- (br_archive & (str_data_name %in% c("consulta_cand", "votacao_partido_munzona", + "bem_candidato", "consulta_vagas", + "detalhe_votacao_munzona", "consulta_legendas"))) + + # Cleans the data + setwd(as.character(year)) + banco <- juntaDados(uf, encoding, br_archive) + setwd("..") + on.exit(unlink(as.character(year), recursive = T)) + + if (!is.null(data_names)) { + names(banco) <- data_names + } + # Change to ascii + if(ascii == T) banco <- to_ascii(banco, encoding) + + # Export + if(export) export_data(banco) + + message("Done.\n") + return(banco) +} + + # Avoid the R CMD check note about magrittr's dot utils::globalVariables(".") - diff --git a/R/vote_mun_zone_fed.R b/R/vote_mun_zone_fed.R index f48b993..2e42973 100644 --- a/R/vote_mun_zone_fed.R +++ b/R/vote_mun_zone_fed.R @@ -80,28 +80,16 @@ #' df <- vote_mun_zone_fed(2002) #' } -vote_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ - - +vote_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FALSE, encoding = "latin1", export = FALSE){ # Test the input test_encoding(encoding) test_fed_year(year) uf <- test_uf(uf) test_br(br_archive) - download_and_unzip_datafile(sprintf("odsele/votacao_candidato_munzona/votacao_candidato_munzona_%s.zip", year), year) - - message("Processing the data...") - - # Clean the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - unlink(as.character(year), recursive = T) - # Change variable names if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "NUMERO_CAND", "SQ_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO", "DESCRICAO_CARGO", "COD_SIT_CAND_SUPERIOR", "DESC_SIT_CAND_SUPERIOR", "CODIGO_SIT_CANDIDATO", @@ -110,15 +98,15 @@ vote_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FAL "TOTAL_VOTOS") }else if(year == 2014) { - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "NUMERO_CAND", "SQ_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO", "DESCRICAO_CARGO", "COD_SIT_CAND_SUPERIOR", "DESC_SIT_CAND_SUPERIOR", "CODIGO_SIT_CANDIDATO", "DESC_SIT_CANDIDATO", "CODIGO_SIT_CAND_TOT", "DESC_SIT_CAND_TOT", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "TOTAL_VOTOS", "TRANSITO") - } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", + } else { + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", @@ -129,13 +117,6 @@ vote_mun_zone_fed <- function(year, uf = "all", br_archive = FALSE, ascii = FAL "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "CODIGO_SIT_CAND_TOT", "DESC_SIT_CAND_TOT", "TRANSITO", "TOTAL_VOTOS") } - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - message("Done.\n") - return(banco) + get_data('votacao_candidato_munzona', year, uf, br_archive, ascii, encoding, export, data_names) } diff --git a/R/vote_mun_zone_local.R b/R/vote_mun_zone_local.R index 5e2eb04..99692fb 100644 --- a/R/vote_mun_zone_local.R +++ b/R/vote_mun_zone_local.R @@ -64,27 +64,16 @@ #' df <- vote_mun_zone_local(2000) #' } -vote_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ - +vote_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "latin1", export = FALSE){ # Test the input test_encoding(encoding) test_local_year(year) uf <- test_uf(uf) - download_and_unzip_datafile(sprintf("odsele/votacao_candidato_munzona/votacao_candidato_munzona_%s.zip", year), year) - - message("Processing the data...") - - # Clean the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - # Change variable names if(year <= 2012){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", "NUMERO_CAND", "SQ_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO", "DESCRICAO_CARGO", "COD_SIT_CAND_SUPERIOR", "DESC_SIT_CAND_SUPERIOR", "CODIGO_SIT_CANDIDATO", @@ -93,7 +82,7 @@ vote_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "la "TOTAL_VOTOS") } else { - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "CODIGO_CARGO", @@ -103,14 +92,7 @@ vote_mun_zone_local <- function(year, uf = "all", ascii = FALSE, encoding = "la "TIPO_AGREMIACAO", "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA", "CODIGO_SIT_CAND_TOT", "DESC_SIT_CAND_TOT", "TRANSITO","TOTAL_VOTOS" ) -} - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) + } - message("Done.\n") - return(banco) + get_data('votacao_candidato_munzona', year, uf, FALSE, ascii, encoding, export, data_names) } diff --git a/R/vote_section_fed.R b/R/vote_section_fed.R index e57bf53..4314c48 100644 --- a/R/vote_section_fed.R +++ b/R/vote_section_fed.R @@ -56,44 +56,27 @@ vote_section_fed <- function(year, uf = "AC", ascii = FALSE, encoding = "latin1", export = FALSE){ - + if(tolower(uf) == "all") { + stop("'uf' is invalid. Please, check the documentation and try again.") + } + # Test the inputs test_encoding(encoding) test_fed_year(year) stopifnot(is.character(uf)) - if(tolower(uf) == "all") stop("'uf' is invalid. Please, check the documentation and try again.") uf <- test_uf(uf) - - download_and_unzip_datafile(sprintf("odsele/votacao_secao/votacao_secao_%s_%s.zip", year, uf), year) - - message("Processing the data...") - - # Clean the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - - # Change variable names + + # Variable names if(year < 2014){ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "NUMERO_SECAO", "CODIGO_CARGO", "DESCRICAO_CARGO", "NUM_VOTAVEL", "QTDE_VOTOS") } else{ - names(banco) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "COD_TIPO_ELEICAO", "NOME_TIPO_ELEICAO", "NUM_TURNO", "COD_ELEICAO", "DESCRICAO_ELEICAO", "DATA_ELEICAO", "ABRANGENCIA", "SIGLA_UF", "SIGLA_UE", "NOME_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "NUMERO_SECAO", "CODIGO_CARGO", "DESCRICAO_CARGO", "NUM_VOTAVEL", "NOME_VOTAVEL", "QTDE_VOTOS") } - - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") - return(banco) + get_data('votacao_secao', year, uf, FALSE, ascii, encoding, export, data_names) } diff --git a/R/vote_section_local.R b/R/vote_section_local.R index f398a52..a15bc51 100644 --- a/R/vote_section_local.R +++ b/R/vote_section_local.R @@ -55,74 +55,35 @@ #' } vote_section_local <- function(year, uf = "AC", ascii = FALSE, encoding = "latin1", export = FALSE){ - - + + if(tolower(uf) == "all") { + stop("'uf' is invalid. Please, check the documentation and try again.") + } + # Test the inputs test_encoding(encoding) test_local_year(year) stopifnot(is.character(uf)) - if(tolower(uf) == "all") stop("'uf' is invalid. Please, check the documentation and try again.") uf <- test_uf(uf) - - if(year < 2012){ - - download_and_unzip_datafile(sprintf("odsele/votacao_secao/votacao_secao_%s_%s.zip", year, uf), year) - - message("Processing the data...") - - # Clean the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - - } else{ - message("Download the data One...") - - download_and_unzip_datafile(sprintf("/eleicoes/eleicoes2012/votosecao/vsec_1t_%s.zip", uf), year) - message("Processing the data one...") - - # Clean the data - setwd(as.character(year)) - banco1 <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - + # Variable names + data_names <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", + "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "NUMERO_SECAO", "CODIGO_CARGO", + "DESCRICAO_CARGO","NUM_VOTAVEL", "QTDE_VOTOS") + + + if (year < 2012) { + banco <- get_data('votacao_secao', year, uf, FALSE, ascii, encoding, export, data_names) + } else { + banco <- get_data('vsec_1t', year, uf, FALSE, ascii, encoding, export, data_names) if(!(uf %in% c("AL", "DF", "GO", "PE", "RR", "SE", "TO"))){ - message("Download the data two...") - - download_and_unzip_datafile(sprintf("eleicoes/eleicoes2012/votosecao/vsec_2t_%s_30102012194527.zip", uf), year) - - message("Processing the data two...") - - # Clean the data - setwd(paste0("./", year, "2")) - banco2 <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(paste0("./", year, "2"), recursive = T) + banco2 <- get_data('vsec_2t_', year, uf, FALSE, ascii, encoding, export, data_names) - }else{ - banco2 <- NULL + banco <- rbind(banco, banco2) } - - banco <- rbind(banco1, banco2) } - - # Change variable names - names(vts_local) <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO", "SIGLA_UF", - "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA", "NUMERO_SECAO", "CODIGO_CARGO", - "DESCRICAO_CARGO","NUM_VOTAVEL", "QTDE_VOTOS") - - # Change to ascii - if(ascii == T) banco <- to_ascii(banco, encoding) - - # Export - if(export) export_data(banco) - - message("Done.\n") return(banco) } From 5cc6513ef9e21369fdf87b1e4ebd11e7bb1df43d Mon Sep 17 00:00:00 2001 From: Andre Felix Date: Wed, 1 Jul 2020 07:33:51 -0300 Subject: [PATCH 3/6] Adiciona opcao de pasta local para armazenamento dos arquivos de dados. Ref #6 --- R/utils.R | 58 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/R/utils.R b/R/utils.R index d8b2d91..f3c0e4e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -35,13 +35,15 @@ parties_br <- function() { # Reads and rbinds multiple data.frames in the same directory #' @import dplyr -juntaDados <- function(uf, encoding, br_archive){ - - archive <- Sys.glob("*")[grepl(".pdf", Sys.glob("*")) == FALSE] %>% - .[grepl(uf, .)] %>% - file.info() %>% - .[.$size > 200, ] %>% - row.names() +juntaDados <- function(str_file_path, uf, encoding, br_archive){ + str_file_path <- gsub(pattern = '(^.*?)(\\/$)', + replacement = '\\1', + x = str_file_path) + archive <- Sys.glob(paste0(str_file_path, "/*"))[grepl(".pdf", Sys.glob(paste0(str_file_path, "/*"))) == FALSE] %>% + .[grepl(uf, .)] %>% + file.info() %>% + .[.$size > 200,] %>% + row.names() if(!br_archive){ @@ -188,23 +190,46 @@ download_and_unzip_datafile <- function(str_endpoint, year) { } get_data <- function(str_data_name, year, uf, br_archive, ascii, encoding, export, data_names = NULL) { + str_data_path <- getOption('electionsBR-data-path') + str_remote_file_location <- get_file_remote_location(str_data_name) str_remote_file_location <- gsub(pattern = '%year%', replacement = year, x = str_remote_file_location) str_remote_file_location <- gsub(pattern = '%uf%', replacement = uf, x = str_remote_file_location) download_and_unzip_datafile(str_remote_file_location, year) - message("Processing the data...") - + str_tmp_dir <- tempdir() + str_file_name <- basename(str_remote_file_location) + + if (is.null(str_data_path)) { + message( + paste( + "Downloading data file to temporary path.", + "To customize data file location set option electionsBR-data-path", + "E.g. options('electionsBR-data-path' = 'my_data_path')", + sep = " \n" + ) + ) + str_file_name <- paste0(str_tmp_dir, "/", str_file_name) + downlaod_file(str_remote_file_location, str_file_name) + } else { + str_file_name <- paste0(str_data_path, "/", str_file_name) + if (!file.exists(str_file_name)) { + downlaod_file(str_remote_file_location, str_file_name) + } + } + br_archive <- (br_archive & (str_data_name %in% c("consulta_cand", "votacao_partido_munzona", "bem_candidato", "consulta_vagas", "detalhe_votacao_munzona", "consulta_legendas"))) - # Cleans the data - setwd(as.character(year)) - banco <- juntaDados(uf, encoding, br_archive) - setwd("..") - on.exit(unlink(as.character(year), recursive = T)) + message("Processing the data...") + tmp_sub_dir <- paste0(str_tmp_dir, "/", year) + unzip(str_file_name, exdir = tmp_sub_dir) + + banco <- juntaDados(tmp_sub_dir, uf, encoding, br_archive) + + on.exit(unlink(str_tmp_dir, recursive = T)) if (!is.null(data_names)) { names(banco) <- data_names @@ -219,6 +244,11 @@ get_data <- function(str_data_name, year, uf, br_archive, ascii, encoding, expor return(banco) } +downlaod_file <- function(str_url_part, str_data_path) { + str_url_base <- 'http://agencia.tse.jus.br/estatistica/sead/%s' + message(sprintf("Downloading data to: %s", str_data_path)) + download.file(sprintf(str_url_base, str_url_part), str_data_path) +} # Avoid the R CMD check note about magrittr's dot utils::globalVariables(".") From c1a13833188aebf4ceb2675bfe3fd2601b755323 Mon Sep 17 00:00:00 2001 From: Andre Felix Date: Wed, 1 Jul 2020 07:44:07 -0300 Subject: [PATCH 4/6] Remove chamada da funcao obsoleta download_and_unzip_datafile --- R/utils.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index f3c0e4e..9cccbe4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -196,8 +196,6 @@ get_data <- function(str_data_name, year, uf, br_archive, ascii, encoding, expor str_remote_file_location <- gsub(pattern = '%year%', replacement = year, x = str_remote_file_location) str_remote_file_location <- gsub(pattern = '%uf%', replacement = uf, x = str_remote_file_location) - download_and_unzip_datafile(str_remote_file_location, year) - str_tmp_dir <- tempdir() str_file_name <- basename(str_remote_file_location) From d1dd08a140b08cf4643ee8c657a3c5fdcecc7722 Mon Sep 17 00:00:00 2001 From: Andre Felix Date: Sat, 11 Jul 2020 11:10:55 -0300 Subject: [PATCH 5/6] Implementa teste de data do arquivo remoto. Corrige caminho remoto na funcao download_and_unzip_datafile --- R/utils.R | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/R/utils.R b/R/utils.R index 4c046d9..efedf97 100644 --- a/R/utils.R +++ b/R/utils.R @@ -171,18 +171,17 @@ get_file_remote_location <- function(str_file_name) { list("name" = "votacao_partido_munzona", "url" = "odsele/votacao_partido_munzona/votacao_partido_munzona_%year%.zip"), list("name" = "detalhe_votacao_munzona", "url" = "odsele/detalhe_votacao_munzona/detalhe_votacao_munzona_%year%.zip"), list("name" = "votacao_secao", "url" = "odsele/votacao_secao/votacao_secao_%year%_%uf%.zip"), - list("name" = "vsec_1t", "url" = "/eleicoes/eleicoes2012/votosecao/vsec_1t_%year%.zip"), + list("name" = "vsec_1t", "url" = "eleicoes/eleicoes2012/votosecao/vsec_1t_%year%.zip"), list("name" = "vsec_2t", "url" = "eleicoes/eleicoes2012/votosecao/vsec_2t_%uf%_30102012194527.zip"), list("name" = "bem_candidato", "url" = "odsele/bem_candidato/bem_candidato_%year%.zip") ) df_file <- do.call(rbind, lapply(lst_file, data.frame)) df_file$url <- as.character(df_file$url) + df_file$url <- paste0('http://agencia.tse.jus.br/estatistica/sead/', df_file$url) return(df_file[df_file$name == str_file_name, c('url')]) } download_and_unzip_datafile <- function(str_endpoint, year) { - str_base_url <- 'http://agencia.tse.jus.br/estatistica/sead/%s' - tmp_data_file <- tempfile() download.file(sprintf(str_base_url, str_endpoint), tmp_data_file) unzip(tmp_data_file, exdir = paste0("./", year)) @@ -212,9 +211,18 @@ get_data <- function(str_data_name, year, uf, br_archive, ascii, encoding, expor downlaod_file(str_remote_file_location, str_file_name) } else { str_file_name <- paste0(str_data_path, "/", str_file_name) + if (!file.exists(str_file_name)) { downlaod_file(str_remote_file_location, str_file_name) } + + if (remote_file_is_newer(str_remote_file_location, str_file_name)) { + print (sprintf("Existe uma nova versao do arquivo %s. Deseja atualizar o arquivo local? [y/n]", basename(str_file_name))) + str_response <- readline() + if (tolower(str_response) != 'n') { + downlaod_file(str_remote_file_location, str_file_name) + } + } } br_archive <- (br_archive & (str_data_name %in% c("consulta_cand", "votacao_partido_munzona", @@ -242,10 +250,18 @@ get_data <- function(str_data_name, year, uf, br_archive, ascii, encoding, expor return(banco) } -downlaod_file <- function(str_url_part, str_data_path) { - str_url_base <- 'http://agencia.tse.jus.br/estatistica/sead/%s' +downlaod_file <- function(str_url, str_data_path) { message(sprintf("Downloading data to: %s", str_data_path)) - download.file(sprintf(str_url_base, str_url_part), str_data_path) + download.file(str_url, str_data_path) +} + +remote_file_is_newer <- function(str_file_remote_url, str_file_local_path) { + response <- httr::HEAD(str_file_remote_url) + + dt_remote <- httr::parse_http_date(response$headers$`last-modified`) + file_info <- file.info(str_file_local_path) + + return(dt_remote >= file_info$mtime) } # Avoid the R CMD check note about magrittr's dot From 73b08d35831888c2a9ee0cff40edebc7a2fcb2d2 Mon Sep 17 00:00:00 2001 From: Andre Felix Date: Sun, 12 Jul 2020 09:51:48 -0300 Subject: [PATCH 6/6] Corrige caminho do download. --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index efedf97..92fcea0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -183,7 +183,7 @@ get_file_remote_location <- function(str_file_name) { download_and_unzip_datafile <- function(str_endpoint, year) { tmp_data_file <- tempfile() - download.file(sprintf(str_base_url, str_endpoint), tmp_data_file) + download.file(str_endpoint, tmp_data_file) unzip(tmp_data_file, exdir = paste0("./", year)) unlink(tmp_data_file) }