-
Notifications
You must be signed in to change notification settings - Fork 1
/
cristin_supported.R
247 lines (211 loc) · 7.08 KB
/
cristin_supported.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
#' @title Check for supported export formats from Cristin to Zotero
#' @description Used to filter Cristin categories that are not supported
#' @param data Zotero-type matrix, Default: NULL
#' @param items Items in library to check for
#' @param zotero.check Should the function look for duplicates?, Default: TRUE
#' @param remove.na Cristin contains many, more or less, obscure categories, and
#' not all are (yet) supported. By default these are removed, however, if this
#' option is set to FALSE unsupported categories are treated according to
#' replace.na, Default: TRUE
#' @param replace.na May the odds be in your favor and replace unsupported
#' categories with a predefined itemType if remove.na is set to false,
#' Default: 'book'
#' @param force.type Force all items to a predefined itemType, Default: NULL
#' @param remove.duplicates Remove duplicates if TRUE, Default: TRUE
#' @param silent c2z is noisy, tell it to be quiet, Default: FALSE
#' @param log A list for storing log elements, Default: list()
#' @return Zotero supported items with unsupported categories as NA
#' @details Please see
#' \href{https://oeysan.github.io/c2z/}{https://oeysan.github.io/c2z/}
#' @examples
#' # Supported Cristin items
#' print(CristinSupported(), n = 5)
#' @seealso
#' \code{\link[tibble]{tibble}}
#' @rdname CristinSupported
#' @export
CristinSupported <- \(data = NULL,
zotero.check = TRUE,
items = NULL,
remove.na = TRUE,
replace.na = "book",
force.type = NULL,
remove.duplicates = TRUE,
silent = FALSE,
log = list()) {
# Visible bindings
unsupported.id <- missing.id <- title <- date_published <- year_published <-
firstName <- creatorType <- lastName <- filter.name <- external.data <-
results <- id <- name <- category <- key <- . <- NULL
# Set replace.na as force.type if defined
if (!is.null(force.type)) replace.na <- force.type
# Currently supported references
supported.types <- c(
anthologyaca= "book",
booktransl = "book",
monographaca= "book",
commentaryaca= "book",
textbook= "book",
nonfictionbook= "book",
popularbook= "book",
chapteracademic= "bookSection",
populararticle= "bookSection",
chapter= "bookSection",
chapterarticle= "bookSection",
foreword= "bookSection",
introduction= "bookSection",
software= "computerProgram",
article= "journalArticle",
academicreview= "journalArticle",
editorial= "journalArticle",
bookreview= "journalArticle",
articlejournal= "journalArticle",
readeropinion= "newspaperArticle",
articlefeature= "newspaperArticle",
articlepopular= "newspaperArticle",
academiclecture= "presentation",
poster= "presentation",
lecture= "presentation",
lecturepopular= "presentation",
otherpres= "presentation",
report= "report",
doctordissertat= "thesis",
thesismaster= "thesis",
thesissecond= "thesis",
thesismagister= "thesis",
medicalthesis= "thesis"
)
# If data is null return list of supported references
if (is.null(data)) {
data <- tibble::tibble(
cristin.category = toupper(names(supported.types)),
zotero.types = supported.types
)
return (data)
}
# Check whether references exists in Zotero library if zotero.check is TRUE
if (any(nrow(data)) & zotero.check & !is.null(items)) {
check <- ZoteroCheck(
data,
id = "cristin_result_id",
id.type = "Cristin",
created = "created",
last.modified = "last_modified",
items = items,
remove.duplicates = remove.duplicates,
silent = silent,
log = log
)
# Set data as checked data
data <- check$data
# Add to log
log <- check$log
}
# Return if all all duplicates
if (!any(nrow(data))) {
return(list(data = data, log = log))
}
# Define Cristin category type
data$type <- GoFish(data$category$name$en)
# Define Cristin category code
data$code <- GoFish(data$category$code)
# Checking references message
log <- LogCat(
"Checking whether references are supported. See `CristinSupported()`",
silent = silent,
log = log
)
# Find supported data
supported.data <- data |>
dplyr::filter(
tolower(category$code) %in% names(supported.types)
) |>
# Replace Cristin categories with supported Zotero types
dplyr::mutate(
category = supported.types[
match(tolower(category$code), names(supported.types))
]
)
# Find unsupported data
unsupported.data <- data |>
dplyr::filter(
!tolower(category$code) %in% names(supported.types)
)
# Run if there are unsupported data
if (nrow(unsupported.data)) {
# Find Cristin ids of unsupported references
unsupported.id <- unsupported.data$cristin_result_id
# Remove unsupported data if remove.na = TRUE
if (remove.na) {
# Removal of unsupported references message
unsupported.message <- sprintf(
"Removed %s unsupported %s",
nrow(unsupported.data),
Pluralis(nrow(unsupported.data), "reference", "references", FALSE)
)
# Else replace with replace.na
} else if (!is.null(replace.na)) {
# Combine unsupported and supported data and replace category
supported.data <- dplyr::bind_rows(
supported.data,
unsupported.data |>
dplyr::mutate(category = replace.na,
part_of = NA)
)
# Converted unsupported references message
unsupported.message <- sprintf(
"Set %s unsupported %s to `%s`",
nrow(unsupported.data),
Pluralis(
nrow(unsupported.data), "reference", "references", FALSE
),
replace.na
)
}
# Log unsupported data
log <- LogCat(
paste(unsupported.message, "See `$unsupported.id`"),
silent = silent,
log = log
)
}
# Set data as supported data
data <- supported.data
# Checking missing data message
log <- LogCat(
"Looking for missing data",
silent = silent,
log = log
)
# filter out data missing title, and/or date
missing.data <- data |>
tidyr::unnest(title, names_sep = "_") |>
dplyr::filter(!dplyr::if_all(c(dplyr::starts_with("title")), is.na),
GoFish(!is.na(date_published)) |
GoFish(!is.na(year_published))) |>
tidyr::nest(title = dplyr::starts_with("title"), .names_sep = "_")
# Check if missing.data has fewer entries than data
if (nrow(data) > nrow(missing.data)) {
# Find Cristin ids of unsupported references
missing.id <- missing.data$cristin_result_id
# Log message if any data is filtered out
log <- LogCat(
sprintf(
"Removed %s with missing data. See `$missing.id`",
Pluralis(nrow(data)-nrow(missing.data), "item", "items")
),
silent = silent,
log = log
)
# Set new data
data <- missing.data
}
return (
list(
data = data,
unsupported.id = unsupported.id,
missing.id = missing.id,
log = log
)
)
}