Skip to content

Commit 458b2e5

Browse files
committed
cleanup citation file and citation function
1 parent c6baf73 commit 458b2e5

File tree

2 files changed

+105
-67
lines changed

2 files changed

+105
-67
lines changed

R/cite.R

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ spod_cite <- function(
2929
format = "all"
3030
) {
3131
# 1. Define valid inputs
32-
valid_what <- c("all", "package", "data", "methodology_v1", "methodology_v2")
32+
valid_what <- c("all", "package", "data", "methodology_v1", "methodology_v2")
3333
valid_format <- c("all", "text", "markdown", "bibtex")
34-
34+
3535
# 2. Use checkmate to validate arguments
3636
checkmate::assertCharacter(what, any.missing = FALSE, min.len = 1)
3737
checkmate::assertCharacter(format, any.missing = FALSE, min.len = 1)
3838
checkmate::assertSubset(what, choices = valid_what)
3939
checkmate::assertSubset(format, choices = valid_format)
40-
40+
4141
# 3. Expand "all" options
4242
# If "all" is included in what, use all sources except the "all" string itself
4343
if ("all" %in% what) {
@@ -47,29 +47,39 @@ spod_cite <- function(
4747
if ("all" %in% format) {
4848
format <- unique(c(format, valid_format[valid_format != "all"]))
4949
}
50-
50+
5151
# Now remove the literal "all" from each to avoid confusion
52-
what <- setdiff(what, "all")
52+
what <- setdiff(what, "all")
5353
format <- setdiff(format, "all")
54-
54+
5555
# 4. Get the citation object
5656
cit <- utils::citation("spanishoddata")
57-
57+
5858
# 5. Function to get citation by key
5959
get_citation_by_key <- function(key) {
6060
idx <- which(sapply(cit, function(x) x$key == key))
61-
if (length(idx) > 0) return(cit[idx])
61+
if (length(idx) > 0) {
62+
return(cit[idx])
63+
}
6264
return(NULL)
6365
}
64-
66+
6567
# 6. Map what options to citation keys
6668
citation_keys <- list(
67-
package = "r-spanishoddata",
68-
data = "mitms_mobility_web",
69+
package = "r-spanishoddata",
70+
data = "mitms_mobility_web",
6971
methodology_v1 = "mitma_methodology_2020_v3",
7072
methodology_v2 = "mitms_methodology_2022_v8"
7173
)
72-
74+
75+
# 6.1. Human‐readable labels for each “what”
76+
citation_labels <- list(
77+
package = "To cite the spanishoddata package",
78+
data = "To site the Ministry's mobility study website",
79+
methodology_v1 = "To cite the methodology for 2020-2021 data",
80+
methodology_v2 = "To cite the methodology for 2022 and onwards data"
81+
)
82+
7383
# 7. Collect the requested citations
7484
citations_to_show <- list()
7585
for (w in what) {
@@ -79,15 +89,15 @@ spod_cite <- function(
7989
citations_to_show[[w]] <- cit_entry
8090
}
8191
}
82-
92+
8393
# If nothing was found (e.g., user gave an empty vector)
8494
if (length(citations_to_show) == 0) {
8595
message("No valid citations found for the requested 'what'.")
8696
return(invisible(NULL))
8797
}
88-
98+
8999
# 8. Helper functions for formatting output
90-
100+
91101
# Plain text
92102
format_text <- function(citation) {
93103
text <- format(citation, style = "text")
@@ -98,7 +108,7 @@ spod_cite <- function(
98108
text <- gsub("<(http[^>]*)>", "\\1", text)
99109
paste(text, collapse = "\n")
100110
}
101-
111+
102112
# Markdown
103113
format_markdown <- function(citation) {
104114
text <- format(citation, style = "text")
@@ -108,27 +118,32 @@ spod_cite <- function(
108118
text <- gsub("<(http[^>]*)>", "\\1", text)
109119
paste(text, collapse = "\n")
110120
}
111-
121+
112122
# 9. Print the citations in requested formats
113123
for (f in format) {
114124
if (f == "text") {
115125
cat("\nPlain text citations:\n---------------------\n")
116-
for (cit_item in citations_to_show) {
126+
for (w in names(citations_to_show)) {
127+
cit_item <- citations_to_show[[w]]
128+
cat(paste0(citation_labels[[w]], ":\n"))
117129
cat(format_text(cit_item), "\n\n")
118130
}
119131
} else if (f == "markdown") {
120132
cat("\nMarkdown citations:\n-------------------\n")
121-
for (cit_item in citations_to_show) {
133+
for (w in names(citations_to_show)) {
134+
cit_item <- citations_to_show[[w]]
135+
cat(paste0("**", citation_labels[[w]], ":**\n"))
122136
cat(format_markdown(cit_item), "\n\n")
123137
}
124138
} else if (f == "bibtex") {
125139
cat("\nBibTeX citations:\n-----------------\n")
126-
for (cit_item in citations_to_show) {
127-
print(utils::toBibtex(cit_item))
140+
for (w in names(citations_to_show)) {
141+
cat(paste0("%% ", citation_labels[[w]], "\n"))
142+
print(utils::toBibtex(citations_to_show[[w]]))
128143
cat("\n")
129144
}
130145
}
131146
}
132-
147+
133148
invisible(NULL)
134149
}

inst/CITATION

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,75 @@
11
c(
2-
bibentry(
3-
header = "To cite the `spanishoddata` R package use:",
4-
key = "r-spanishoddata",
5-
bibtype = "Manual",
6-
title = "spanishoddata",
7-
author = c(
8-
person("Egor", "Kotov", , "kotov.egor@gmail.com", role = c("aut", "cre"),
9-
comment = c(ORCID = "0000-0001-6690-5345")),
10-
person("Robin", "Lovelace", , "rob00x@gmail.com", role = "aut",
11-
comment = c(ORCID = "0000-0001-5679-6536")),
12-
person("Eugeni", "Vidal-Tortosa", role = "ctb",
13-
comment = c(ORCID = "0000-0001-5199-4103"))
2+
bibentry(
3+
header = "To cite the `spanishoddata` R package use:",
4+
key = "r-spanishoddata",
5+
bibtype = "Manual",
6+
title = "spanishoddata",
7+
author = c(
8+
person(
9+
"Egor",
10+
"Kotov",
11+
,
12+
"kotov.egor@gmail.com",
13+
role = c("aut", "cre"),
14+
comment = c(ORCID = "0000-0001-6690-5345")
15+
),
16+
person(
17+
"Robin",
18+
"Lovelace",
19+
,
20+
"rob00x@gmail.com",
21+
role = "aut",
22+
comment = c(ORCID = "0000-0001-5679-6536")
23+
),
24+
person(
25+
"Eugeni",
26+
"Vidal-Tortosa",
27+
role = "ctb",
28+
comment = c(ORCID = "0000-0001-5199-4103")
29+
)
30+
),
31+
year = "2024",
32+
url = "https://github.com/rOpenSpain/spanishoddata",
33+
doi = "10.32614/CRAN.package.spanishoddata"
1434
),
15-
year = "2024",
16-
url = "https://github.com/rOpenSpain/spanishoddata",
17-
doi = "10.32614/CRAN.package.spanishoddata"
18-
),
1935

36+
bibentry(
37+
header = "To cite the official website of the mobility study use:",
38+
key = "mitms_mobility_web",
39+
bibtype = "Misc",
40+
title = "Estudio de la movilidad con Big Data (Study of mobility with Big Data)",
41+
author = person(
42+
"Ministerio de Transportes y Movilidad Sostenible (MITMS)"
43+
),
44+
year = "2024",
45+
url = "https://www.transportes.gob.es/ministerio/proyectos-singulares/estudio-de-movilidad-con-big-data",
46+
),
2047

21-
bibentry(
22-
header = "To cite the official website of the mobility study use:",
23-
key = "mitms_mobility_web",
24-
bibtype = "Misc",
25-
title = "Estudio de la movilidad con Big Data (Study of mobility with Big Data)",
26-
author = person("Ministerio de Transportes y Movilidad Sostenible (MITMS)"),
27-
year = "2024",
28-
url = "https://www.transportes.gob.es/ministerio/proyectos-singulares/estudio-de-movilidad-con-big-data",
29-
),
30-
31-
bibentry(
32-
header = "To cite the methodology for 2022 and onwards data use:",
33-
key = "mitms_methodology_2022_v8",
34-
bibtype = "Manual",
35-
title = "Estudio de movilidad de viajeros de ámbito nacional aplicando la tecnología Big Data. Informe metodológico (Study of National Traveler mobility Using Big Data Technology. Methodological Report)",
36-
author = person("Ministerio de Transportes y Movilidad Sostenible (MITMS)"),
37-
year = "2024",
38-
url = "https://www.transportes.gob.es/recursos_mfom/paginabasica/recursos/a3_informe_metodologico_estudio_movilidad_mitms_v8.pdf",
39-
),
48+
bibentry(
49+
header = "To cite the methodology for 2022 and onwards data use:",
50+
key = "mitms_methodology_2022_v8",
51+
bibtype = "Manual",
52+
title = "Estudio de movilidad de viajeros de ámbito nacional aplicando la tecnología Big Data. Informe metodológico (Study of National Traveler mobility Using Big Data Technology. Methodological Report)",
53+
author = person(
54+
"Ministerio de Transportes y Movilidad Sostenible (MITMS)"
55+
),
56+
year = "2024",
57+
url = "https://www.transportes.gob.es/recursos_mfom/paginabasica/recursos/a3_informe_metodologico_estudio_movilidad_mitms_v8.pdf",
58+
),
4059

41-
bibentry(
42-
header = "To cite the methodology for 2020-2021 data use:",
43-
key = "mitma_methodology_2020_v3",
44-
bibtype = "Manual",
45-
title = "Análisis de la movilidad en España con tecnología Big Data durante el estado de alarma para la gestión de la crisis del COVID-19 (Analysis of mobility in Spain with Big Data technology during the state of alarm for COVID-19 crisis management)",
46-
author = person("Ministerio de Transportes, Movilidad y Agenda Urbana (MITMA)"),
47-
year = "2021",
48-
url = "https://cdn.mitma.gob.es/portal-web-drupal/covid-19/bigdata/mitma_-_estudio_movilidad_covid-19_informe_metodologico_v3.pdf",
49-
)
60+
bibentry(
61+
header = "To cite the methodology for 2020-2021 data use:",
62+
key = "mitma_methodology_2020_v3",
63+
bibtype = "Manual",
64+
title = "Análisis de la movilidad en España con tecnología Big Data durante el estado de alarma para la gestión de la crisis del COVID-19 (Analysis of mobility in Spain with Big Data technology during the state of alarm for COVID-19 crisis management)",
65+
author = person(
66+
"Ministerio de Transportes, Movilidad y Agenda Urbana (MITMA)"
67+
),
68+
year = "2021",
69+
url = "https://cdn.mitma.gob.es/portal-web-drupal/covid-19/bigdata/mitma_-_estudio_movilidad_covid-19_informe_metodologico_v3.pdf",
70+
)
5071
)
5172

52-
citFooter("See package website for more details: https://ropenspain.github.io/spanishoddata/")
73+
citFooter(
74+
"See package website for more details: https://ropenspain.github.io/spanishoddata/"
75+
)

0 commit comments

Comments
 (0)