/
plume-quarto.R
266 lines (248 loc) · 7.58 KB
/
plume-quarto.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
.names_quarto <- list_modify(.names, public = list(
internals = list(
deceased = "deceased",
equal_contributor = "equal_contributor"
),
secondaries = list(
number = "number",
dropping_particle = "dropping_particle",
acknowledgements = "acknowledgements"
)
))
#' @title PlumeQuarto class
#' @description Class that pushes author metadata in the YAML header
#' of Quarto files.
#' @examples
#' # Create a simple temporary file with a YAML header
#' # containing a title
#' tmp_file <- tempfile(fileext = ".qmd")
#' readr::write_lines("---\ntitle: Encyclopédie\n---", tmp_file)
#'
#' # View the temporary file
#' cat(readr::read_file(tmp_file))
#'
#' # Create a PlumeQuarto instance using the temporary file
#' # you've just created
#' aut <- PlumeQuarto$new(
#' encyclopedists,
#' file = tmp_file
#' )
#'
#' # And push author data to the YAML header
#' aut$to_yaml()
#'
#' cat(readr::read_file(tmp_file))
#'
#' # Pushing again with new data updates the YAML
#' # header accordingly
#' aut <- PlumeQuarto$new(
#' dplyr::slice(encyclopedists, 2),
#' file = tmp_file
#' )
#' aut$to_yaml()
#'
#' cat(readr::read_file(tmp_file))
#'
#' # Clean up the temporary file
#' unlink(tmp_file)
#' @export
PlumeQuarto <- R6Class(
classname = "PlumeQuarto",
inherit = StatusSetterPlumeQuarto,
public = list(
#' @description Create a `PlumeQuarto` object.
#' @param data A data frame containing author-related data.
#' @param file A `.qmd` file to insert author data into.
#' @param names A vector of key-value pairs specifying custom names to use,
#' where keys are default names and values their respective replacements.
#' @param roles A vector of key-value pairs defining roles where keys
#' identify columns and values describe the actual roles to use.
#' @param credit_roles `r lifecycle::badge("deprecated")`
#'
#' It is now recommended to use `roles = credit_roles()` to use the
#' `r link("crt")`.
#' @param initials_given_name Should the initials of given names be used?
#' @param by A character string defining the default variable used to assign
#' specific metadata to authors in all `set_*()` methods. By default, uses
#' authors' id.
#' @return A `PlumeQuarto` object.
initialize = function(
data,
file,
names = NULL,
roles = credit_roles(),
credit_roles = FALSE,
initials_given_name = FALSE,
by = NULL
) {
check_file(file, extension = "qmd")
super$initialize(data, names, roles, credit_roles, initials_given_name, by = by)
private$file <- file
private$id <- private$pick("id")
},
#' @description Push or update author information in a YAML header. The
#' generated YAML complies with Quarto's `r link("quarto_schemas")`.
#' @details
#' If missing, `to_yaml()` pushes author information into a YAML header. If
#' already existing, the function replaces old `author` and `affiliations`
#' values with the ones provided in the input data.
#' @return The input `file` invisibly.
to_yaml = function() {
yaml_push(private$get_template(), file = private$file)
}
),
private = list(
file = NULL,
plume_names = .names_quarto,
meta_prefix = "meta-",
id = NULL,
mold = function(...) {
super$mold(starts_with(private$meta_prefix), ...)
},
get_template = function() {
list(
author = private$author_tbl(),
affiliations = private$affiliation_tbl()
)
},
author_tbl = function() {
tibble(
id = private$author_ids(),
number = private$get("number"),
name = tibble(
given = private$get("given_name"),
family = private$get("family_name"),
`dropping-particle` = private$get("dropping_particle")
),
url = private$get("url"),
email = private$get("email"),
phone = private$get("phone"),
fax = private$get("fax"),
orcid = private$author_orcids(),
note = private$author_notes(),
acknowledgements = private$get("acknowledgements"),
attributes = private$author_attributes(),
roles = private$author_roles(),
metadata = private$author_metadata(),
affiliations = private$author_affiliations()
)
},
author_ids = function() {
ids <- private$get("id")
if (length(ids) == 1L) {
return()
}
paste0("aut", ids)
},
author_orcids = function() {
out <- private$get("orcid")
if (!is.null(out)) {
check_orcid(out)
}
out
},
author_roles = function() {
col <- private$pick("role")
if (!private$has_col(col)) {
return()
}
out <- unnest_drop(private$plume, cols = all_of(col))
out <- summarise(
out,
`_` = list(tolower(.data[[col]])),
.by = all_of(private$id)
)
out[["_"]]
},
author_notes = function() {
col <- private$pick("note")
if (!private$has_col(col)) {
return()
}
if (!is_nested(private$plume, col)) {
return(private$get("note"))
}
out <- unnest(private$plume, cols = all_of(col))
out <- summarise(out, `_` = if_not_na(
.data[[col]],
bind(.data[[col]], sep = ", ", arrange = FALSE),
all = TRUE
), .by = all_of(private$id))
out[["_"]]
},
author_attributes = function() {
out <- tibble(
corresponding = private$get("corresponding"),
deceased = private$get("deceased"),
`equal-contributor` = private$get("equal_contributor")
)
if (is_empty(out)) {
return()
}
out
},
author_affiliations = function() {
col <- private$pick("affiliation")
if (!private$has_col(col)) {
return()
}
.col <- predot(col)
out <- unnest(private$plume, cols = all_of(col))
out <- add_group_ids(out, col)
out <- mutate(out, !!.col := if_not_na(
.data[[.col]],
paste0("aff", .data[[.col]])
))
out <- summarise(out, `_` = list(
tibble(ref = sort(!!sym(.col)))
), .by = all_of(private$id))
out[["_"]]
},
author_metadata = function() {
if (!private$has_col(begins_with(private$meta_prefix))) {
return()
}
select(private$plume, starts_with(private$meta_prefix))
},
affiliation_tbl = function() {
affiliations <- private$get("affiliation")
if (is.null(affiliations)) {
return()
}
affiliations <- condense(affiliations)
if (!any(has_affiliation_sep(affiliations))) {
ids <- make_affiliation_id(affiliations)
return(tibble(id = ids, name = affiliations))
}
out <- map(affiliations, \(affiliation) {
as_tibble_row(parse_affiliation(affiliation))
})
out <- list_rbind(out, names_to = "id")
out <- mutate(out, id = make_affiliation_id(id))
if (is_empty(out)) {
return()
}
out
}
)
)
affiliation_keys <- c(
"number", "name", "department", "address", "city", "region", "state",
"country", "postal-code", "url", "isni", "ringgold", "ror"
)
parse_affiliation <- function(x) {
if (!has_affiliation_sep(x)) {
return(set_names(x, "name"))
}
keys <- collapse(affiliation_keys, sep = "|")
keys_regex <- paste0("\\b(?i:", keys, ")")
nms <- str_extract_all(x, sprintf("%s(?==)", keys_regex), simplify = TRUE)
els <- str_split_1(x, sprintf("\\s*%s=\\s*", keys_regex))[-1]
set_names(els, tolower(nms))
}
make_affiliation_id <- function(x) {
paste0("aff", seq_along(x))
}
has_affiliation_sep <- function(x) {
str_contain(x, "=")
}