From d7477243995537519f2f406cb04b5c469cde96f4 Mon Sep 17 00:00:00 2001 From: Meleiro Date: Mon, 27 May 2024 11:52:20 +0200 Subject: [PATCH] Adding user agent header in vignettes and examples to resubmit to CRAN --- DESCRIPTION | 2 +- R/candidatos_nosenado.R | 16 +++++++++------- R/examples/candidatos.R | 13 ++++++++----- R/examples/mesas.R | 2 +- R/examples/municipios.R | 10 ++++++---- R/examples/provincias.R | 9 +++++---- R/mesas.R | 10 +++++----- R/muni.R | 8 ++++---- R/provincias.R | 8 ++++---- R/senado_mesas.R | 19 ++++++++++--------- R/senado_municipios.R | 19 ++++++++++--------- man/candidatos.Rd | 13 ++++++++----- man/mesas.Rd | 2 +- man/municipios.Rd | 10 ++++++---- man/provincias.Rd | 9 +++++---- tests/testthat/test-candidatos.R | 2 +- tests/testthat/test-mesas.R | 2 ++ tests/testthat/test-municipios.R | 1 + tests/testthat/test-provincias.R | 1 + vignettes/infoelectoral.Rmd | 14 ++++++++++---- vignettes/municipios.Rmd | 3 ++- 21 files changed, 100 insertions(+), 73 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 30f986e..8241471 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: infoelectoral Type: Package Title: Download Spanish Election Results -Version: 1.0.0 +Version: 1.0.1 Authors@R: person("Héctor", "Meleiro", email = "hmeleiros@gmail.com", diff --git a/R/candidatos_nosenado.R b/R/candidatos_nosenado.R index c948436..19bfd21 100644 --- a/R/candidatos_nosenado.R +++ b/R/candidatos_nosenado.R @@ -14,6 +14,8 @@ #' @importFrom dplyr select #' @importFrom dplyr arrange #' @importFrom dplyr %>% +#' @importFrom dplyr full_join +#' @importFrom dplyr left_join #' #' @keywords internal #' @@ -22,14 +24,14 @@ candidatos_nosenado <- function(tipo, anno, mes) { urlbase <- "https://infoelectoral.interior.gob.es/estaticos/docxl/apliextr/" url <- paste0(urlbase, tipo, anno, mes, "_MUNI", ".zip") ### Descargo el fichero zip en un directorio temporal y lo descomprimo - tempd <- tempdir(check = F) + tempd <- tempdir(check = FALSE) temp <- tempfile(tmpdir = tempd, fileext = ".zip") download.file(url, temp, mode = "wb") - unzip(temp, overwrite = T, exdir = tempd) + unzip(temp, overwrite = TRUE, exdir = tempd) ### Construyo las rutas a los ficheros DAT necesarios codigo_eleccion <- paste0(substr(anno, nchar(anno)-1, nchar(anno)), mes) - todos <- list.files(tempd, recursive = T) + todos <- list.files(tempd, recursive = TRUE) x <- todos[grepl(paste0("04", tipo, codigo_eleccion, ".DAT"), todos)] xbasicos <- todos[grepl(paste0("05", tipo, codigo_eleccion, ".DAT"), todos)] xcandidaturas <- todos[grepl(paste0("03", tipo, codigo_eleccion, ".DAT"), todos)] @@ -44,12 +46,12 @@ candidatos_nosenado <- function(tipo, anno, mes) { dfbasicos <- dfbasicos[dfbasicos$codigo_distrito == "99",] ### Limpio el directorio temporal (IMPORTANTE: Si no lo hace, puede haber problemas al descargar más de una elección) - borrar <- list.files(tempd, full.names = T, recursive = T) - try(file.remove(borrar), silent = T) + borrar <- list.files(tempd, full.names = TRUE, recursive = TRUE) + try(file.remove(borrar), silent = TRUE) ### Junto los datos de los tres ficheros - df <- merge(dfbasicos, dfcandidatos, by = c("tipo_eleccion", "vuelta", "anno", "mes", "codigo_provincia", "codigo_municipio", "codigo_distrito"), all = T) - df <- merge(df, dfcandidaturas, by = c("tipo_eleccion", "anno", "mes", "codigo_partido"), all.x = T) + df <- full_join(dfbasicos, dfcandidatos, by = c("tipo_eleccion", "vuelta", "anno", "mes", "codigo_provincia", "codigo_municipio", "codigo_distrito")) + df <- left_join(df, dfcandidaturas, by = c("tipo_eleccion", "anno", "mes", "codigo_partido")) ### Limpieza: Quito los espacios en blanco a los lados de estas variables df$siglas <- str_trim(df$siglas) diff --git a/R/examples/candidatos.R b/R/examples/candidatos.R index c5c4d2c..8d72c5d 100644 --- a/R/examples/candidatos.R +++ b/R/examples/candidatos.R @@ -1,5 +1,8 @@ -\dontrun{ - data <- candidatos(tipo_eleccion = "senado", anno = "2004", - mes = "03", nivel = "municipio") - str(data) -} +ua <- "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)" +options(HTTPUserAgent = ua) + +data <- candidatos( + tipo_eleccion = "senado", anno = "2004", + mes = "03", nivel = "municipio" +) +str(data) diff --git a/R/examples/mesas.R b/R/examples/mesas.R index 6d481a1..98725bd 100644 --- a/R/examples/mesas.R +++ b/R/examples/mesas.R @@ -1,4 +1,4 @@ -\dontrun{ +\donttest{ data <- mesas(tipo_eleccion = "congreso", anno = "2023", mes = "07") str(data) } diff --git a/R/examples/municipios.R b/R/examples/municipios.R index d7afa2f..4e975d5 100644 --- a/R/examples/municipios.R +++ b/R/examples/municipios.R @@ -1,4 +1,6 @@ -\dontrun{ - data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11") - str(data) -} +ua <- "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)" +options(HTTPUserAgent = ua) + +data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11") +str(data) + diff --git a/R/examples/provincias.R b/R/examples/provincias.R index 35e2c64..583f529 100644 --- a/R/examples/provincias.R +++ b/R/examples/provincias.R @@ -1,4 +1,5 @@ -\dontrun{ - data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10") - str(data) -} +ua <- "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)" +options(HTTPUserAgent = ua) + +data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10") +str(data) diff --git a/R/mesas.R b/R/mesas.R index aef703c..33eba3d 100644 --- a/R/mesas.R +++ b/R/mesas.R @@ -36,14 +36,14 @@ mesas <- function(tipo_eleccion, anno, mes) { url <- paste0(urlbase, tipo, anno, mes, "_MESA", ".zip") ### Descargo el fichero zip en un directorio temporal y lo descomprimo - tempd <- tempdir(check = F) + tempd <- tempdir(check = FALSE) temp <- tempfile(tmpdir = tempd, fileext = ".zip") download.file(url, temp, mode = "wb") - unzip(temp, overwrite = T, exdir = tempd) + unzip(temp, overwrite = TRUE, exdir = tempd) ### Construyo las rutas a los ficheros DAT necesarios codigo_eleccion <- paste0(substr(anno, nchar(anno)-1, nchar(anno)), mes) - todos <- list.files(tempd, recursive = T) + todos <- list.files(tempd, recursive = TRUE) x <- todos[grepl(paste0("10", tipo, codigo_eleccion, ".DAT"), todos)] xbasicos <- todos[grepl(paste0("09", tipo, codigo_eleccion, ".DAT"), todos)] xcandidaturas <- todos[grepl(paste0("03", tipo, codigo_eleccion, ".DAT"), todos)] @@ -54,8 +54,8 @@ mesas <- function(tipo_eleccion, anno, mes) { dfmesas <- read10(x, tempd) ### Limpio el directorio temporal (IMPORTANTE: Si no lo hace, puede haber problemas al descargar más de una elección) - borrar <- list.files(tempd, full.names = T, recursive = T) - try(file.remove(borrar), silent = T) + borrar <- list.files(tempd, full.names = TRUE, recursive = TRUE) + try(file.remove(borrar), silent = TRUE) ### Junto los datos de los tres ficheros diff --git a/R/muni.R b/R/muni.R index dd67eec..1a162af 100644 --- a/R/muni.R +++ b/R/muni.R @@ -43,11 +43,11 @@ municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) { temp <- tempfile(tmpdir = tempd, fileext = ".zip") download.file(url, temp, mode = "wb") - unzip(temp, overwrite = T, exdir = tempd) + unzip(temp, overwrite = TRUE, exdir = tempd) ### Construyo las rutas a los ficheros DAT necesarios codigo_eleccion <- paste0(substr(anno, nchar(anno)-1, nchar(anno)), mes) - todos <- list.files(tempd, recursive = T) + todos <- list.files(tempd, recursive = TRUE) x <- todos[grepl(paste0("06", tipo, codigo_eleccion, ".DAT"), todos)] xbasicos <- todos[grepl(paste0("05", tipo, codigo_eleccion, ".DAT"), todos)] xcandidaturas <- todos[grepl(paste0("03", tipo, codigo_eleccion, ".DAT"), todos)] @@ -73,8 +73,8 @@ municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) { } ### Limpio el directorio temporal (IMPORTANTE: Si no lo hace, puede haber problemas al descargar más de una elección) - borrar <- list.files(tempd, full.names = T, recursive = T) - try(file.remove(borrar), silent = T) + borrar <- list.files(tempd, full.names = TRUE, recursive = TRUE) + try(file.remove(borrar), silent = TRUE) ### Junto los datos de los tres ficheros df <- full_join(dfbasicos, dfmunicipios, diff --git a/R/provincias.R b/R/provincias.R index 98605d0..5784d60 100644 --- a/R/provincias.R +++ b/R/provincias.R @@ -40,12 +40,12 @@ provincias <- function(tipo_eleccion, anno, mes) { tempd <- tempdir() temp <- tempfile(tmpdir = tempd, fileext = ".zip") download.file(url, temp, mode = "wb") - unzip(temp, overwrite = T, exdir = tempd) + unzip(temp, overwrite = TRUE, exdir = tempd) ### Construyo las rutas a los ficheros DAT necesarios codigo_eleccion <- paste0(substr(anno, nchar(anno)-1, nchar(anno)), mes) - todos <- list.files(tempd, recursive = T) + todos <- list.files(tempd, recursive = TRUE) x <- todos[todos == paste0("03", tipo, codigo_eleccion, ".DAT")] xbasicos <- todos[todos == paste0("07", tipo, codigo_eleccion, ".DAT")] xcandidaturas <- todos[todos == paste0("08", tipo, codigo_eleccion, ".DAT")] @@ -58,8 +58,8 @@ provincias <- function(tipo_eleccion, anno, mes) { ### Limpio el directorio temporal (IMPORTANTE: Si no lo hace, puede haber problemas al descargar más de una elección) - borrar <- list.files(tempd, full.names = T, recursive = T) - try(file.remove(borrar), silent = T) + borrar <- list.files(tempd, full.names = TRUE, recursive = TRUE) + try(file.remove(borrar), silent = TRUE) ### Junto los datos de los tres ficheros df <- full_join(dfcandidaturas_basicos, dfcandidaturas, diff --git a/R/senado_mesas.R b/R/senado_mesas.R index e4f6f24..d2dad48 100644 --- a/R/senado_mesas.R +++ b/R/senado_mesas.R @@ -16,6 +16,7 @@ #' @importFrom dplyr select #' @importFrom dplyr arrange #' @importFrom dplyr %>% +#' @importFrom dplyr full_join #' #' @keywords internal senado_mesas <- function(anno, mes) { @@ -27,14 +28,14 @@ senado_mesas <- function(anno, mes) { url <- paste0(urlbase, tipo, anno, mes, "_MESA", ".zip") ### Descargo el fichero zip en un directorio temporal y lo descomprimo - tempd <- tempdir(check = F) + tempd <- tempdir(check = FALSE) temp <- tempfile(tmpdir = tempd, fileext = ".zip") download.file(url, temp, mode = "wb") - unzip(temp, overwrite = T, exdir = tempd) + unzip(temp, overwrite = TRUE, exdir = tempd) ### Construyo las rutas a los ficheros DAT necesarios codigo_eleccion <- paste0(substr(anno, nchar(anno)-1, nchar(anno)), mes) - todos <- list.files(tempd, recursive = T) + todos <- list.files(tempd, recursive = TRUE) x <- todos[todos == paste0("04", tipo, codigo_eleccion, ".DAT")] xmesas <- todos[todos == paste0("10", tipo, codigo_eleccion, ".DAT")] xbasicos <- todos[todos == paste0("09", tipo, codigo_eleccion, ".DAT")] @@ -47,17 +48,17 @@ senado_mesas <- function(anno, mes) { dfmesas <- read10(xmesas, tempd) ### Limpio el directorio temporal (IMPORTANTE: Si no lo hace, puede haber problemas al descargar más de una elección) - borrar <- list.files(tempd, full.names = T, recursive = T) - try(file.remove(borrar), silent = T) + borrar <- list.files(tempd, full.names = TRUE, recursive = TRUE) + try(file.remove(borrar), silent = TRUE) ### Junto los datos de los tres ficheros - df <- merge(dfbasicos, dfmesas, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_ccaa", "codigo_provincia", "codigo_municipio", "codigo_distrito", "codigo_seccion", "codigo_mesa"), all = T) - df <- merge(df, dfcandidatos, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_provincia", "codigo_senador"), all = T) - df <- merge(df, dfcandidaturas, by = c("tipo_eleccion", "anno", "mes", "codigo_partido"), all = T) + df <- full_join(dfbasicos, dfmesas, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_ccaa", "codigo_provincia", "codigo_municipio", "codigo_distrito", "codigo_seccion", "codigo_mesa")) + df <- full_join(df, dfcandidatos, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_provincia", "codigo_senador")) + df <- full_join(df, dfcandidaturas, by = c("tipo_eleccion", "anno", "mes", "codigo_partido")) # Inserto el nombre del municipio más reciente y reordeno algunas variables codigos_municipios <- infoelectoral::codigos_municipios - df <- merge(df, codigos_municipios, by = c("codigo_provincia", "codigo_municipio"), all = T) + df <- full_join(df, codigos_municipios, by = c("codigo_provincia", "codigo_municipio")) ### Limpieza: Quito los espacios en blanco a los lados de estas variables df$siglas <- str_trim(df$siglas) diff --git a/R/senado_municipios.R b/R/senado_municipios.R index 9083c81..d11ecc1 100644 --- a/R/senado_municipios.R +++ b/R/senado_municipios.R @@ -16,6 +16,7 @@ #' @importFrom dplyr select #' @importFrom dplyr arrange #' @importFrom dplyr %>% +#' @importFrom dplyr full_join #' #' @keywords internal senado_municipios <- function(anno, mes) { @@ -26,14 +27,14 @@ senado_municipios <- function(anno, mes) { url <- paste0(urlbase, tipo, anno, mes, "_MUNI", ".zip") ### Descargo el fichero zip en un directorio temporal y lo descomprimo - tempd <- tempdir(check = F) + tempd <- tempdir(check = FALSE) temp <- tempfile(tmpdir = tempd, fileext = ".zip") download.file(url, temp, mode = "wb") - unzip(temp, overwrite = T, exdir = tempd) + unzip(temp, overwrite = TRUE, exdir = tempd) ### Construyo las rutas a los ficheros DAT necesarios codigo_eleccion <- paste0(substr(anno, nchar(anno)-1, nchar(anno)), mes) - todos <- list.files(tempd, recursive = T) + todos <- list.files(tempd, recursive = TRUE) x <- todos[todos == paste0("04", tipo, codigo_eleccion, ".DAT")] xmunicipios <- todos[todos == paste0("06", tipo, codigo_eleccion, ".DAT")] xbasicos <- todos[todos == paste0("05", tipo, codigo_eleccion, ".DAT")] @@ -50,20 +51,20 @@ senado_municipios <- function(anno, mes) { ### Limpio el directorio temporal (IMPORTANTE: Si no lo hace, puede haber problemas al descargar más de una elección) - borrar <- list.files(tempd, full.names = T, recursive = T) - try(file.remove(borrar), silent = T) + borrar <- list.files(tempd, full.names = TRUE, recursive = TRUE) + try(file.remove(borrar), silent = TRUE) ### Junto los datos de los tres ficheros - df <- merge(dfbasicos, dfmunicipios, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_provincia", "codigo_municipio", "codigo_distrito"), all = T) - df <- merge(df, dfcandidatos, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_provincia", "codigo_distrito_electoral", "codigo_senador"), all = T) - df <- merge(df, dfcandidaturas, by = c("tipo_eleccion", "anno", "mes", "codigo_partido"), all = T) + df <- full_join(dfbasicos, dfmunicipios, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_provincia", "codigo_municipio", "codigo_distrito")) + df <- full_join(df, dfcandidatos, by = c("tipo_eleccion", "anno", "mes", "vuelta", "codigo_provincia", "codigo_distrito_electoral", "codigo_senador")) + df <- full_join(df, dfcandidaturas, by = c("tipo_eleccion", "anno", "mes", "codigo_partido")) ### Limpieza: Quito los espacios en blanco a los lados de estas variables df$siglas <- str_trim(df$siglas) df$denominacion <- str_trim(df$denominacion) codigos_municipios <- infoelectoral::codigos_municipios - df <- merge(df, codigos_municipios, by = c("codigo_provincia", "codigo_municipio"), all = T) %>% + df <- full_join(df, codigos_municipios, by = c("codigo_provincia", "codigo_municipio")) %>% mutate_if(is.character, str_trim) %>% mutate(denominacion = str_remove_all("denominacion", '"')) %>% select( diff --git a/man/candidatos.Rd b/man/candidatos.Rd index d30d3db..5849f92 100644 --- a/man/candidatos.Rd +++ b/man/candidatos.Rd @@ -22,9 +22,12 @@ data.frame with the candidates data. If tipo_eleccion = "senado" a column called `candidatos()` downloads, formats and imports to the environment the data of the candidates from the electoral lists of the selected elections. } \examples{ -\dontrun{ - data <- candidatos(tipo_eleccion = "senado", anno = "2004", - mes = "03", nivel = "municipio") - str(data) -} +ua <- "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)" +options(HTTPUserAgent = ua) + +data <- candidatos( + tipo_eleccion = "senado", anno = "2004", + mes = "03", nivel = "municipio" +) +str(data) } diff --git a/man/mesas.Rd b/man/mesas.Rd index 22a51ca..3ab74c9 100644 --- a/man/mesas.Rd +++ b/man/mesas.Rd @@ -20,7 +20,7 @@ data.frame with the electoral results data at the polling station level. `mesas()` downloads, formats and imports to the environment the electoral results data of the selected election at the polling station level. } \examples{ -\dontrun{ +\donttest{ data <- mesas(tipo_eleccion = "congreso", anno = "2023", mes = "07") str(data) } diff --git a/man/municipios.Rd b/man/municipios.Rd index c056559..74352e4 100644 --- a/man/municipios.Rd +++ b/man/municipios.Rd @@ -22,8 +22,10 @@ Dataframe with the electoral results data at the municipality level. `municipios()` downloads, formats and imports to the environment the electoral results data of the selected election at the municipality level. } \examples{ -\dontrun{ - data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11") - str(data) -} +ua <- "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)" +options(HTTPUserAgent = ua) + +data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11") +str(data) + } diff --git a/man/provincias.Rd b/man/provincias.Rd index a51a43c..0090e47 100644 --- a/man/provincias.Rd +++ b/man/provincias.Rd @@ -20,8 +20,9 @@ data.frame with the electoral results data at the polling station level. `provincias()` downloads, formats and imports to the environment the electoral results data of the selected election at electoral constituency level (province or island). } \examples{ -\dontrun{ - data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10") - str(data) -} +ua <- "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)" +options(HTTPUserAgent = ua) + +data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10") +str(data) } diff --git a/tests/testthat/test-candidatos.R b/tests/testthat/test-candidatos.R index e158dd5..dd56871 100644 --- a/tests/testthat/test-candidatos.R +++ b/tests/testthat/test-candidatos.R @@ -1,4 +1,5 @@ library(dplyr) +options(HTTPUserAgent="Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") set.seed(120) x <- fechas_elecciones %>% @@ -9,7 +10,6 @@ x <- fechas_elecciones %>% data <- candidatos("senado", x$anno, x$mes, nivel = "municipio") %>% filter(codigo_provincia != "99") - test_that("n_provincias function", { n_provincias <- unique(data$codigo_provincia) %>% length() expect_equal(n_provincias, 52) diff --git a/tests/testthat/test-mesas.R b/tests/testthat/test-mesas.R index a89612d..f7f596b 100644 --- a/tests/testthat/test-mesas.R +++ b/tests/testthat/test-mesas.R @@ -1,4 +1,6 @@ library(dplyr) +# options(timeout = 120) +options(HTTPUserAgent="Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") set.seed(120) x <- fechas_elecciones %>% diff --git a/tests/testthat/test-municipios.R b/tests/testthat/test-municipios.R index 4ce653c..f917e7c 100644 --- a/tests/testthat/test-municipios.R +++ b/tests/testthat/test-municipios.R @@ -1,4 +1,5 @@ library(dplyr) +options(HTTPUserAgent="Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") set.seed(120) x <- fechas_elecciones %>% diff --git a/tests/testthat/test-provincias.R b/tests/testthat/test-provincias.R index 4827b0a..6838186 100644 --- a/tests/testthat/test-provincias.R +++ b/tests/testthat/test-provincias.R @@ -1,4 +1,5 @@ library(dplyr) +options(HTTPUserAgent="Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") set.seed(120) x <- fechas_elecciones %>% diff --git a/vignettes/infoelectoral.Rmd b/vignettes/infoelectoral.Rmd index 58cdf4a..e8c94da 100644 --- a/vignettes/infoelectoral.Rmd +++ b/vignettes/infoelectoral.Rmd @@ -8,35 +8,41 @@ vignette: > ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = T, results = T, include = T, warning = F, message = F) +options(HTTPUserAgent="Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") + ``` The goal of [infoelectoral](https://ropenspain.github.io/infoelectoral/) is to facilitate the downloading and necessary processing of official data on electoral results from the Ministry of the Interior of Spain. Files can be downloaded at the polling station and municipality level on the ministry's website. However, the format of the files (plain text without delimitation) and the data structure design (the useful information for the analysis of the elections is dispersed in at least three different files) make importing the data somewhat costly. -To download the data at the municipality level, you have the `municipalities ()` function to which you have to provide the type of desired election ("congreso", "senado", "municipales" or "europeas", the Ministry of the Interior does not have the data from the regional electoral processes), the year (YYYY) and the month (MM) of the election. +To download the data at the municipality level, you have the `municipalities ()` function to which you have to provide the type of desired election ("congreso", "senado", "municipales" or "europeas", the Ministry of the Interior does not have the data from the regional electoral processes), the year (YYYY) and the month (MM) of the election. + ```{r} library(infoelectoral) library(dplyr) df <- municipios(tipo_eleccion = "congreso", anno = "1982", mes = "10") glimpse(df) +# Sys.sleep(4) ``` To download election data at the polling station level: + ```{r} df <- mesas("congreso", "2019", "04") glimpse(df) +# Sys.sleep(4) ``` -The [infoelectoral](https://ropenspain.github.io/infoelectoral/) library also has the function `candidatos()`, which allows the downloading of data on candidates for the Senate, the Congress of Deputies, the European Parliament and city councils. In the case of Senate candidates, the function requires a value in the argument `nivel` indicating whether the results are wanted at the municipality or polling station level, and the resulting `data.frame` will include a column called `votos` indicating the number of ballots received by each candidate for the Upper House. +The [infoelectoral](https://ropenspain.github.io/infoelectoral/) library also has the function `candidatos()`, which allows the downloading of data on candidates for the Senate, the Congress of Deputies, the European Parliament and city councils. In the case of Senate candidates, the function requires a value in the argument `nivel` indicating whether the results are wanted at the municipality or polling station level, and the resulting `data.frame` will include a column called `votos` indicating the number of ballots received by each candidate for the Upper House. ```{r} df <- candidatos(tipo_eleccion = "senado", anno = "2019", mes = "11", nivel = "municipio") glimpse(df) +# Sys.sleep(4) ``` -In the case of candidates for the Congress of Deputies, European Parliament or city councils, institutions elected through closed lists, it is not necessary to provide a value for the argument `nivel` and the resulting object will not have the column `votos`. +In the case of candidates for the Congress of Deputies, European Parliament or city councils, institutions elected through closed lists, it is not necessary to provide a value for the argument `nivel` and the resulting object will not have the column `votos`. ```{r} df <- candidatos("europeas", "2019", "05") glimpse(df) ``` - diff --git a/vignettes/municipios.Rmd b/vignettes/municipios.Rmd index 499f569..d40f02c 100644 --- a/vignettes/municipios.Rmd +++ b/vignettes/municipios.Rmd @@ -9,6 +9,7 @@ vignette: > ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = T, results = T, include = T, warning = F, message = F) +options(HTTPUserAgent="Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") ``` ## Install and load the packages @@ -27,7 +28,7 @@ library(tidyr) ## Download the results -Download some results. In this case we download the election for Congress of decembre 2015. +Download some results. In this case we download the election for Congress of december 2015. ```{r} results <- municipios("congreso", "2015", "12") # Descargo los datos