diff --git a/R/candidate_fed.R b/R/candidate_fed.R index 9e3c9e0..bebe428 100644 --- a/R/candidate_fed.R +++ b/R/candidate_fed.R @@ -114,31 +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 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) - - 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", @@ -152,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", @@ -168,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 01ae4d9..41368b7 100644 --- a/R/candidate_local.R +++ b/R/candidate_local.R @@ -92,25 +92,10 @@ 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) - - 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", @@ -122,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", @@ -136,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", @@ -154,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 22fd73b..ae6a10c 100644 --- a/R/details_mun_zone_fed.R +++ b/R/details_mun_zone_fed.R @@ -84,31 +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) - # 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) - - 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", @@ -116,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", @@ -125,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", @@ -133,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 a27a715..30ed731 100644 --- a/R/details_mun_zone_local.R +++ b/R/details_mun_zone_local.R @@ -68,30 +68,14 @@ 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) - - 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", @@ -99,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", @@ -108,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 7b28137..24ac117 100644 --- a/R/legend_fed.R +++ b/R/legend_fed.R @@ -64,56 +64,31 @@ 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) 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" + data_name <- "consulta_legendas" } else{ - endereco <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_coligacao/consulta_coligacao_%s.zip" + data_name <- "consulta_coligacao" } - # Download the data - dados <- tempfile() - sprintf(endereco, year) %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) - - 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 5d273ec..af67063 100644 --- a/R/legend_local.R +++ b/R/legend_local.R @@ -58,45 +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 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) - - 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 a6082af..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,50 +83,28 @@ 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) - - 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 831fd9b..91f1a89 100644 --- a/R/party_mun_zone_local.R +++ b/R/party_mun_zone_local.R @@ -64,51 +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 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) - - 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 d998b09..4015b36 100644 --- a/R/personal_finances_fed.R +++ b/R/personal_finances_fed.R @@ -62,53 +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") - - # 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) - - 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) -} - - \ No newline at end of file + # 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") + } + + 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 cdc5f50..eae4be6 100644 --- a/R/personal_finances_local.R +++ b/R/personal_finances_local.R @@ -46,50 +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) - - # 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) - - 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 3cbc22b..a521a5c 100644 --- a/R/seats_fed.R +++ b/R/seats_fed.R @@ -53,50 +53,25 @@ #' 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) 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) - - 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 308f41b..66f4aa7 100644 --- a/R/seats_local.R +++ b/R/seats_local.R @@ -51,46 +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 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) - - 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 18e05ca..92fcea0 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){ @@ -159,7 +161,108 @@ 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) + 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) { + tmp_data_file <- tempfile() + download.file(str_endpoint, tmp_data_file) + unzip(tmp_data_file, exdir = paste0("./", year)) + unlink(tmp_data_file) +} + +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) + + 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) + } + + 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", + "bem_candidato", "consulta_vagas", + "detalhe_votacao_munzona", "consulta_legendas"))) + + 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 + } + # Change to ascii + if(ascii == T) banco <- to_ascii(banco, encoding) + + # Export + if(export) export_data(banco) + + message("Done.\n") + return(banco) +} + +downlaod_file <- function(str_url, str_data_path) { + message(sprintf("Downloading data to: %s", 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 utils::globalVariables(".") - diff --git a/R/vote_mun_zone_fed.R b/R/vote_mun_zone_fed.R index e02b8c1..2e42973 100644 --- a/R/vote_mun_zone_fed.R +++ b/R/vote_mun_zone_fed.R @@ -80,33 +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 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) - - 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", @@ -115,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", @@ -134,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 c40658f..99692fb 100644 --- a/R/vote_mun_zone_local.R +++ b/R/vote_mun_zone_local.R @@ -64,32 +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 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) - - 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", @@ -98,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", @@ -108,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 68c6545..4314c48 100644 --- a/R/vote_section_fed.R +++ b/R/vote_section_fed.R @@ -56,49 +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 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) - - 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 78f33d2..a15bc51 100644 --- a/R/vote_section_local.R +++ b/R/vote_section_local.R @@ -55,87 +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) + + # 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){ - - # 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) - - 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...") - - 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) - - message("Processing the data one...") - - # Clean the data - setwd(as.character(year)) - banco1 <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(as.character(year), recursive = T) - + + 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"))){ + + banco2 <- get_data('vsec_2t_', year, uf, FALSE, ascii, encoding, export, data_names) - 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) - - message("Processing the data two...") - - # Clean the data - setwd(paste0("./", year, "2")) - banco2 <- juntaDados(uf, encoding, FALSE) - setwd("..") - unlink(paste0("./", year, "2"), recursive = T) - - }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) } diff --git a/R/voter_profile.R b/R/voter_profile.R index bf3bf2f..9ac3ba5 100644 --- a/R/voter_profile.R +++ b/R/voter_profile.R @@ -45,21 +45,19 @@ voter_profile <- function(year, ascii = FALSE, encoding = "windows-1252", export if(!year %in% seq(1994, 2020, by = 2)) stop("Invalid 'year'. Please check the documentation and try again.") test_encoding(encoding) - # Download data - dados <- tempfile() - - if(year == 2020){ - urldir <- "http://agencia.tse.jus.br/estatistica/sead/odsele/perfil_eleitorado/perfil_eleitorado_ATUAL.zip" - } else{ - urldir <- sprintf("http://agencia.tse.jus.br/estatistica/sead/odsele/perfil_eleitorado/perfil_eleitorado_%s.zip", year) - - } - - urldir %>% - download.file(dados) - unzip(dados, exdir = paste0("./", year)) - unlink(dados) + if (year == 2020) { + urldir <- + "http://agencia.tse.jus.br/estatistica/sead/odsele/perfil_eleitorado/perfil_eleitorado_ATUAL.zip" + } else { + urldir <- + sprintf( + "http://agencia.tse.jus.br/estatistica/sead/odsele/perfil_eleitorado/perfil_eleitorado_%s.zip", + year + ) + } + download_and_unzip_datafile(urldir, year) + # Join data message("Processing the data...")