Skip to content

Commit

Permalink
Modifications, examples and tests to submit to CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
hmeleiro committed Mar 8, 2024
1 parent 69f9248 commit d5d85bc
Show file tree
Hide file tree
Showing 26 changed files with 406 additions and 190 deletions.
18 changes: 14 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ Package: infoelectoral
Type: Package
Title: Download Spanish Election Results
Version: 1.0.0
Author: Héctor Meleiro
Authors@R:
person("Héctor", "Meleiro",
email = "[email protected]",
role = c("aut", "cre"))
Maintainer: Héctor Meleiro <[email protected]>
Description: Download official election results for Spain at polling station, municipality and province level from the Ministry of Interior, format them and import them to the R environment.
Description: Download official election results for Spain at polling station, municipality and province level from the Ministry of Interior (<https://infoelectoral.interior.gob.es/es/elecciones-celebradas/area-de-descargas/>), format them and import them to the R environment.
Depends: R (>= 3.5.0)
License: GPL-2
URL: https://github.com/rOpenSpain/infoelectoral
Encoding: UTF-8
LazyData: true
Imports: rlang (>= 0.4.0), dplyr (>= 1.0.0), stringr (>= 1.0.0)
Suggests: mapSpain, tmap, tidyr, preferably, knitr
Imports: dplyr (>= 1.0.0), stringr (>= 1.0.0)
Suggests:
mapSpain,
tmap,
tidyr,
preferably,
knitr,
testthat (>= 3.0.0)
RoxygenNote: 7.2.3
VignetteBuilder: knitr
Config/testthat/edition: 3
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ export(provincias)
importFrom(dplyr,"%>%")
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,desc)
importFrom(dplyr,full_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_if)
importFrom(dplyr,relocate)
importFrom(dplyr,select)
importFrom(rlang,.data)
importFrom(stringr,str_remove_all)
importFrom(stringr,str_trim)
importFrom(utils,data)
importFrom(utils,download.file)
importFrom(utils,unzip)
2 changes: 2 additions & 0 deletions R/candidatos.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' @param mes The month of the election in MM format.
#' @param nivel The administrative level for which the data is wanted ("mesa" for polling stations or "municipio" for municipalities). Only necessary when tipo_eleccion = "senado"
#'
#' @example R/examples/candidatos.R
#'
#' @return data.frame with the candidates data. If tipo_eleccion = "senado" a column called `votos` is included with the votes recieved by each candidate. If other type of election is selected this column is not included since the votes are not received by the specific candidates but by the closed list of the party.
#'
#' @importFrom stringr str_trim
Expand Down
52 changes: 27 additions & 25 deletions R/candidatos_nosenado.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,34 @@ candidatos_nosenado <- function(tipo, anno, mes) {

df <- df %>%
mutate_if(is.character, str_trim) %>%
mutate(denominacion = str_remove_all(.data$denominacion, '"')) %>%
mutate(denominacion = str_remove_all("denominacion", '"')) %>%
select(
.data$tipo_eleccion,
.data$anno,
.data$mes,
.data$vuelta,
.data$codigo_provincia,
.data$codigo_municipio,
.data$codigo_distrito,
.data$orden_candidato,
.data$tipo_candidato,
.data$nombre,
.data$apellido_1,
.data$apellido_2,
.data$sexo,
.data$nacimiento,
.data$dni ,
.data$electo,
.data$codigo_partido_nacional,
.data$codigo_partido_autonomia,
.data$codigo_partido_provincia,
.data$codigo_partido,
.data$denominacion,
.data$siglas
)%>%
arrange(.data$codigo_provincia, .data$siglas, .data$orden_candidato)
"tipo_eleccion",
"anno",
"mes",
"vuelta",
"codigo_provincia",
"codigo_municipio",
"codigo_distrito",
"orden_candidato",
"tipo_candidato",
"nombre",
"apellido_1",
"apellido_2",
"sexo",
"nacimiento",
"dni" ,
"electo",
"codigo_partido_nacional",
"codigo_partido_autonomia",
"codigo_partido_provincia",
"codigo_partido",
"denominacion",
"siglas"
) %>%
arrange("codigo_provincia", "siglas", "orden_candidato")

df <- df[!is.na(df$orden_candidato),]

df$nacimiento <- NA
df$dni <- NA
Expand Down
14 changes: 14 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@
#' }
"codigos_partidos"

#' Election dates
#'
#' This dataset contains the dates of the local, general and european elections in Spain from 1977 to 2023.
#'
#' @format A dataset with the dates of the elections
#' \describe{
#' \item{tipo_eleccion}{Election level (local, general or european parliament)}
#' \item{anno}{Year of the election}
#' \item{mes}{Month of the election}
#' \item{day}{Day of the election}
#' }
#' @source \url{https://www.juntaelectoralcentral.es/cs/jec/elecciones}
"fechas_elecciones"

#' Mean income at the census section level (INE)
#'
#' This dataset contains the mean income of each census section
Expand Down
5 changes: 5 additions & 0 deletions R/examples/candidatos.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\dontrun{
data <- candidatos(tipo_eleccion = "senado", anno = "2004",
mes = "03", nivel = "municipio")
str(data)
}
4 changes: 4 additions & 0 deletions R/examples/mesas.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\dontrun{
data <- mesas(tipo_eleccion = "congreso", anno = "2023", mes = "07")
str(data)
}
4 changes: 4 additions & 0 deletions R/examples/municipios.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\dontrun{
data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11")
str(data)
}
4 changes: 4 additions & 0 deletions R/examples/provincias.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\dontrun{
data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10")
str(data)
}
50 changes: 26 additions & 24 deletions R/mesas.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#' @param anno The year of the election in YYYY format.
#' @param mes The month of the election in MM format.
#'
#' @example R/examples/mesas.R
#'
#' @return data.frame with the electoral results data at the polling station level.
#'
#' @importFrom stringr str_trim
#' @importFrom stringr str_remove_all
#' @importFrom dplyr relocate
#' @importFrom dplyr desc
#' @importFrom dplyr full_join
#' @importFrom dplyr left_join
#' @importFrom dplyr %>%
#' @importFrom rlang .data
#'
#' @export
mesas <- function(tipo_eleccion, anno, mes) {
Expand Down Expand Up @@ -70,32 +72,32 @@ mesas <- function(tipo_eleccion, anno, mes) {
codigos_municipios <- infoelectoral::codigos_municipios
df <- left_join(df, codigos_municipios, by = c("codigo_provincia", "codigo_municipio")) %>%
relocate(
.data$codigo_ccaa,
.data$codigo_provincia,
.data$codigo_municipio,
.data$municipio,
.data$codigo_distrito,
.data$codigo_seccion,
.data$codigo_mesa,
.after = .data$vuelta) %>%
"codigo_ccaa",
"codigo_provincia",
"codigo_municipio",
"municipio",
"codigo_distrito",
"codigo_seccion",
"codigo_mesa",
.after = "vuelta") %>%
relocate(
.data$codigo_partido_autonomia,
.data$codigo_partido_provincia,
.data$codigo_partido,
.data$denominacion,
.data$siglas,
.data$votos,
.data$datos_oficiales ,
.after = .data$codigo_partido_nacional
"codigo_partido_autonomia",
"codigo_partido_provincia",
"codigo_partido",
"denominacion",
"siglas",
"votos",
"datos_oficiales" ,
.after = "codigo_partido_nacional"
) %>%
arrange(
.data$codigo_ccaa,
.data$codigo_provincia,
.data$codigo_municipio,
.data$codigo_distrito,
.data$codigo_seccion,
.data$codigo_mesa,
-.data$votos
"codigo_ccaa",
"codigo_provincia",
"codigo_municipio",
"codigo_distrito",
"codigo_seccion",
"codigo_mesa",
desc("votos")
)

df$municipio[df$codigo_municipio == "999"] <- "CERA"
Expand Down
42 changes: 21 additions & 21 deletions R/muni.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' @param mes The month of the election in MM format.
#' @param distritos Should district level results be returned when available? The default is FALSE. Please be aware when summarizing the data that districts = TRUE will return separate rows for the total municipal level and for each of the districts.
#'
#' @example R/examples/municipios.R
#'
#' @return Dataframe with the electoral results data at the municipality level.
#'
#' @importFrom stringr str_trim
Expand All @@ -16,8 +18,6 @@
#' @importFrom dplyr bind_rows
#' @importFrom dplyr full_join
#' @importFrom dplyr left_join
#' @importFrom rlang .data
#' @importFrom utils data
#' @export
#'
municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) {
Expand Down Expand Up @@ -94,26 +94,26 @@ municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) {
df <- left_join(df, codigos_municipios,
by = c("codigo_provincia", "codigo_municipio")) %>%
relocate(
.data$codigo_ccaa,
.data$codigo_provincia,
.data$codigo_municipio,
.data$municipio,
.data$codigo_distrito,
.data$codigo_distrito_electoral,
.data$codigo_partido_judicial,
.data$codigo_diputacion,
.data$codigo_comarca,
.after = .data$vuelta) %>%
"codigo_ccaa",
"codigo_provincia",
"codigo_municipio",
"municipio",
"codigo_distrito",
"codigo_distrito_electoral",
"codigo_partido_judicial",
"codigo_diputacion",
"codigo_comarca",
.after = "vuelta") %>%
relocate(
.data$codigo_partido_autonomia,
.data$codigo_partido_provincia,
.data$codigo_partido,
.data$denominacion,
.data$siglas,
.data$votos,
.data$datos_oficiales ,
.data$concejales_obtenidos,
.after = .data$codigo_partido_nacional
"codigo_partido_autonomia",
"codigo_partido_provincia",
"codigo_partido",
"denominacion",
"siglas",
"votos",
"datos_oficiales",
"concejales_obtenidos",
.after = "codigo_partido_nacional"
)

### Si no se quieren los distritos se eliminan de los datos
Expand Down
42 changes: 22 additions & 20 deletions R/provincias.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#' @param anno The year of the election in YYYY format.
#' @param mes The month of the election in MM format.
#'
#' @example R/examples/provincias.R
#'
#' @return data.frame with the electoral results data at the polling station level.
#'
#' @importFrom stringr str_trim
#' @importFrom stringr str_remove_all
#' @importFrom dplyr relocate
#' @importFrom dplyr desc
#' @importFrom dplyr full_join
#' @importFrom dplyr left_join
#' @importFrom dplyr %>%
#' @importFrom rlang .data
#'
#' @export
provincias <- function(tipo_eleccion, anno, mes) {
Expand Down Expand Up @@ -78,30 +80,30 @@ provincias <- function(tipo_eleccion, anno, mes) {
# Inserto el nombre del municipio más reciente y reordeno algunas variables
df <- df %>%
relocate(
.data$codigo_ccaa,
.data$codigo_provincia,
.data$codigo_distrito_electoral,
.after = .data$vuelta) %>%
"codigo_ccaa",
"codigo_provincia",
"codigo_distrito_electoral",
.after = "vuelta") %>%
relocate(
.data$codigo_partido_autonomia,
.data$codigo_partido_provincia,
.data$codigo_partido,
.data$denominacion,
.data$siglas,
.data$votos,
.data$diputados,
.data$datos_oficiales,
.after = .data$codigo_partido_nacional
"codigo_partido_autonomia",
"codigo_partido_provincia",
"codigo_partido",
"denominacion",
"siglas",
"votos",
"diputados",
"datos_oficiales",
.after = "codigo_partido_nacional"
) %>%
relocate(
.data$n_diputados,
.after = .data$poblacion_derecho
"n_diputados",
.after = "poblacion_derecho"
) %>%
arrange(
.data$codigo_ccaa,
.data$codigo_provincia,
.data$codigo_distrito_electoral,
-.data$votos
"codigo_ccaa",
"codigo_provincia",
"codigo_distrito_electoral",
desc("votos")
)

return(df)
Expand Down
Loading

0 comments on commit d5d85bc

Please sign in to comment.