Skip to content

Commit d5d85bc

Browse files
committed
Modifications, examples and tests to submit to CRAN
1 parent 69f9248 commit d5d85bc

26 files changed

+406
-190
lines changed

DESCRIPTION

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ Package: infoelectoral
22
Type: Package
33
Title: Download Spanish Election Results
44
Version: 1.0.0
5-
Author: Héctor Meleiro
5+
Authors@R:
6+
person("Héctor", "Meleiro",
7+
email = "[email protected]",
8+
role = c("aut", "cre"))
69
Maintainer: Héctor Meleiro <[email protected]>
7-
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.
10+
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.
811
Depends: R (>= 3.5.0)
912
License: GPL-2
1013
URL: https://github.com/rOpenSpain/infoelectoral
1114
Encoding: UTF-8
1215
LazyData: true
13-
Imports: rlang (>= 0.4.0), dplyr (>= 1.0.0), stringr (>= 1.0.0)
14-
Suggests: mapSpain, tmap, tidyr, preferably, knitr
16+
Imports: dplyr (>= 1.0.0), stringr (>= 1.0.0)
17+
Suggests:
18+
mapSpain,
19+
tmap,
20+
tidyr,
21+
preferably,
22+
knitr,
23+
testthat (>= 3.0.0)
1524
RoxygenNote: 7.2.3
1625
VignetteBuilder: knitr
26+
Config/testthat/edition: 3

NAMESPACE

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ export(provincias)
77
importFrom(dplyr,"%>%")
88
importFrom(dplyr,arrange)
99
importFrom(dplyr,bind_rows)
10+
importFrom(dplyr,desc)
1011
importFrom(dplyr,full_join)
1112
importFrom(dplyr,left_join)
1213
importFrom(dplyr,mutate)
1314
importFrom(dplyr,mutate_if)
1415
importFrom(dplyr,relocate)
1516
importFrom(dplyr,select)
16-
importFrom(rlang,.data)
1717
importFrom(stringr,str_remove_all)
1818
importFrom(stringr,str_trim)
19-
importFrom(utils,data)
2019
importFrom(utils,download.file)
2120
importFrom(utils,unzip)

R/candidatos.R

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#' @param mes The month of the election in MM format.
88
#' @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"
99
#'
10+
#' @example R/examples/candidatos.R
11+
#'
1012
#' @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.
1113
#'
1214
#' @importFrom stringr str_trim

R/candidatos_nosenado.R

+27-25
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,34 @@ candidatos_nosenado <- function(tipo, anno, mes) {
5757

5858
df <- df %>%
5959
mutate_if(is.character, str_trim) %>%
60-
mutate(denominacion = str_remove_all(.data$denominacion, '"')) %>%
60+
mutate(denominacion = str_remove_all("denominacion", '"')) %>%
6161
select(
62-
.data$tipo_eleccion,
63-
.data$anno,
64-
.data$mes,
65-
.data$vuelta,
66-
.data$codigo_provincia,
67-
.data$codigo_municipio,
68-
.data$codigo_distrito,
69-
.data$orden_candidato,
70-
.data$tipo_candidato,
71-
.data$nombre,
72-
.data$apellido_1,
73-
.data$apellido_2,
74-
.data$sexo,
75-
.data$nacimiento,
76-
.data$dni ,
77-
.data$electo,
78-
.data$codigo_partido_nacional,
79-
.data$codigo_partido_autonomia,
80-
.data$codigo_partido_provincia,
81-
.data$codigo_partido,
82-
.data$denominacion,
83-
.data$siglas
84-
)%>%
85-
arrange(.data$codigo_provincia, .data$siglas, .data$orden_candidato)
62+
"tipo_eleccion",
63+
"anno",
64+
"mes",
65+
"vuelta",
66+
"codigo_provincia",
67+
"codigo_municipio",
68+
"codigo_distrito",
69+
"orden_candidato",
70+
"tipo_candidato",
71+
"nombre",
72+
"apellido_1",
73+
"apellido_2",
74+
"sexo",
75+
"nacimiento",
76+
"dni" ,
77+
"electo",
78+
"codigo_partido_nacional",
79+
"codigo_partido_autonomia",
80+
"codigo_partido_provincia",
81+
"codigo_partido",
82+
"denominacion",
83+
"siglas"
84+
) %>%
85+
arrange("codigo_provincia", "siglas", "orden_candidato")
86+
87+
df <- df[!is.na(df$orden_candidato),]
8688

8789
df$nacimiento <- NA
8890
df$dni <- NA

R/data.R

+14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@
5252
#' }
5353
"codigos_partidos"
5454

55+
#' Election dates
56+
#'
57+
#' This dataset contains the dates of the local, general and european elections in Spain from 1977 to 2023.
58+
#'
59+
#' @format A dataset with the dates of the elections
60+
#' \describe{
61+
#' \item{tipo_eleccion}{Election level (local, general or european parliament)}
62+
#' \item{anno}{Year of the election}
63+
#' \item{mes}{Month of the election}
64+
#' \item{day}{Day of the election}
65+
#' }
66+
#' @source \url{https://www.juntaelectoralcentral.es/cs/jec/elecciones}
67+
"fechas_elecciones"
68+
5569
#' Mean income at the census section level (INE)
5670
#'
5771
#' This dataset contains the mean income of each census section

R/examples/candidatos.R

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
\dontrun{
2+
data <- candidatos(tipo_eleccion = "senado", anno = "2004",
3+
mes = "03", nivel = "municipio")
4+
str(data)
5+
}

R/examples/mesas.R

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\dontrun{
2+
data <- mesas(tipo_eleccion = "congreso", anno = "2023", mes = "07")
3+
str(data)
4+
}

R/examples/municipios.R

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\dontrun{
2+
data <- municipios(tipo_eleccion = "congreso", anno = "2019", mes = "11")
3+
str(data)
4+
}

R/examples/provincias.R

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\dontrun{
2+
data <- provincias(tipo_eleccion = "congreso", anno = "1982", mes = "10")
3+
str(data)
4+
}

R/mesas.R

+26-24
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
#' @param anno The year of the election in YYYY format.
77
#' @param mes The month of the election in MM format.
88
#'
9+
#' @example R/examples/mesas.R
10+
#'
911
#' @return data.frame with the electoral results data at the polling station level.
1012
#'
1113
#' @importFrom stringr str_trim
1214
#' @importFrom stringr str_remove_all
1315
#' @importFrom dplyr relocate
16+
#' @importFrom dplyr desc
1417
#' @importFrom dplyr full_join
1518
#' @importFrom dplyr left_join
1619
#' @importFrom dplyr %>%
17-
#' @importFrom rlang .data
1820
#'
1921
#' @export
2022
mesas <- function(tipo_eleccion, anno, mes) {
@@ -70,32 +72,32 @@ mesas <- function(tipo_eleccion, anno, mes) {
7072
codigos_municipios <- infoelectoral::codigos_municipios
7173
df <- left_join(df, codigos_municipios, by = c("codigo_provincia", "codigo_municipio")) %>%
7274
relocate(
73-
.data$codigo_ccaa,
74-
.data$codigo_provincia,
75-
.data$codigo_municipio,
76-
.data$municipio,
77-
.data$codigo_distrito,
78-
.data$codigo_seccion,
79-
.data$codigo_mesa,
80-
.after = .data$vuelta) %>%
75+
"codigo_ccaa",
76+
"codigo_provincia",
77+
"codigo_municipio",
78+
"municipio",
79+
"codigo_distrito",
80+
"codigo_seccion",
81+
"codigo_mesa",
82+
.after = "vuelta") %>%
8183
relocate(
82-
.data$codigo_partido_autonomia,
83-
.data$codigo_partido_provincia,
84-
.data$codigo_partido,
85-
.data$denominacion,
86-
.data$siglas,
87-
.data$votos,
88-
.data$datos_oficiales ,
89-
.after = .data$codigo_partido_nacional
84+
"codigo_partido_autonomia",
85+
"codigo_partido_provincia",
86+
"codigo_partido",
87+
"denominacion",
88+
"siglas",
89+
"votos",
90+
"datos_oficiales" ,
91+
.after = "codigo_partido_nacional"
9092
) %>%
9193
arrange(
92-
.data$codigo_ccaa,
93-
.data$codigo_provincia,
94-
.data$codigo_municipio,
95-
.data$codigo_distrito,
96-
.data$codigo_seccion,
97-
.data$codigo_mesa,
98-
-.data$votos
94+
"codigo_ccaa",
95+
"codigo_provincia",
96+
"codigo_municipio",
97+
"codigo_distrito",
98+
"codigo_seccion",
99+
"codigo_mesa",
100+
desc("votos")
99101
)
100102

101103
df$municipio[df$codigo_municipio == "999"] <- "CERA"

R/muni.R

+21-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#' @param mes The month of the election in MM format.
88
#' @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.
99
#'
10+
#' @example R/examples/municipios.R
11+
#'
1012
#' @return Dataframe with the electoral results data at the municipality level.
1113
#'
1214
#' @importFrom stringr str_trim
@@ -16,8 +18,6 @@
1618
#' @importFrom dplyr bind_rows
1719
#' @importFrom dplyr full_join
1820
#' @importFrom dplyr left_join
19-
#' @importFrom rlang .data
20-
#' @importFrom utils data
2121
#' @export
2222
#'
2323
municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) {
@@ -94,26 +94,26 @@ municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) {
9494
df <- left_join(df, codigos_municipios,
9595
by = c("codigo_provincia", "codigo_municipio")) %>%
9696
relocate(
97-
.data$codigo_ccaa,
98-
.data$codigo_provincia,
99-
.data$codigo_municipio,
100-
.data$municipio,
101-
.data$codigo_distrito,
102-
.data$codigo_distrito_electoral,
103-
.data$codigo_partido_judicial,
104-
.data$codigo_diputacion,
105-
.data$codigo_comarca,
106-
.after = .data$vuelta) %>%
97+
"codigo_ccaa",
98+
"codigo_provincia",
99+
"codigo_municipio",
100+
"municipio",
101+
"codigo_distrito",
102+
"codigo_distrito_electoral",
103+
"codigo_partido_judicial",
104+
"codigo_diputacion",
105+
"codigo_comarca",
106+
.after = "vuelta") %>%
107107
relocate(
108-
.data$codigo_partido_autonomia,
109-
.data$codigo_partido_provincia,
110-
.data$codigo_partido,
111-
.data$denominacion,
112-
.data$siglas,
113-
.data$votos,
114-
.data$datos_oficiales ,
115-
.data$concejales_obtenidos,
116-
.after = .data$codigo_partido_nacional
108+
"codigo_partido_autonomia",
109+
"codigo_partido_provincia",
110+
"codigo_partido",
111+
"denominacion",
112+
"siglas",
113+
"votos",
114+
"datos_oficiales",
115+
"concejales_obtenidos",
116+
.after = "codigo_partido_nacional"
117117
)
118118

119119
### Si no se quieren los distritos se eliminan de los datos

R/provincias.R

+22-20
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
#' @param anno The year of the election in YYYY format.
77
#' @param mes The month of the election in MM format.
88
#'
9+
#' @example R/examples/provincias.R
10+
#'
911
#' @return data.frame with the electoral results data at the polling station level.
1012
#'
1113
#' @importFrom stringr str_trim
1214
#' @importFrom stringr str_remove_all
1315
#' @importFrom dplyr relocate
16+
#' @importFrom dplyr desc
1417
#' @importFrom dplyr full_join
1518
#' @importFrom dplyr left_join
1619
#' @importFrom dplyr %>%
17-
#' @importFrom rlang .data
1820
#'
1921
#' @export
2022
provincias <- function(tipo_eleccion, anno, mes) {
@@ -78,30 +80,30 @@ provincias <- function(tipo_eleccion, anno, mes) {
7880
# Inserto el nombre del municipio más reciente y reordeno algunas variables
7981
df <- df %>%
8082
relocate(
81-
.data$codigo_ccaa,
82-
.data$codigo_provincia,
83-
.data$codigo_distrito_electoral,
84-
.after = .data$vuelta) %>%
83+
"codigo_ccaa",
84+
"codigo_provincia",
85+
"codigo_distrito_electoral",
86+
.after = "vuelta") %>%
8587
relocate(
86-
.data$codigo_partido_autonomia,
87-
.data$codigo_partido_provincia,
88-
.data$codigo_partido,
89-
.data$denominacion,
90-
.data$siglas,
91-
.data$votos,
92-
.data$diputados,
93-
.data$datos_oficiales,
94-
.after = .data$codigo_partido_nacional
88+
"codigo_partido_autonomia",
89+
"codigo_partido_provincia",
90+
"codigo_partido",
91+
"denominacion",
92+
"siglas",
93+
"votos",
94+
"diputados",
95+
"datos_oficiales",
96+
.after = "codigo_partido_nacional"
9597
) %>%
9698
relocate(
97-
.data$n_diputados,
98-
.after = .data$poblacion_derecho
99+
"n_diputados",
100+
.after = "poblacion_derecho"
99101
) %>%
100102
arrange(
101-
.data$codigo_ccaa,
102-
.data$codigo_provincia,
103-
.data$codigo_distrito_electoral,
104-
-.data$votos
103+
"codigo_ccaa",
104+
"codigo_provincia",
105+
"codigo_distrito_electoral",
106+
desc("votos")
105107
)
106108

107109
return(df)

0 commit comments

Comments
 (0)