-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.r
72 lines (46 loc) · 1.85 KB
/
helper.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
#--- helper functions ---#
library(stringr)
# tabContent
# this function generates HTML code from a R list to be displayed below table
# x: R list to be converted to HTML code
tabContent <- function(x) {
tabHeaders <- names(x) |>
str_replace_all("_", " ") |>
str_to_sentence()
null_terms <- c("NA", "N/A", "not stated", "not specified", "not clearly stated",
"not applicable", "see above", "irrelevant to study",
"not mentioned", "none stated", "no specific")
out <- paste0("<b>", tabHeaders[1], ":</b><br>", x[1], "<br>")
for(i in 2:length(x)) {
if(is.null(x[i]) | x[i] == "" | x[i] == " " | x[i] == "No" | x[i] == "No." |
x[i] == "no" | x[i] == "None" | x[i] == "none") {
condition <- TRUE
} else {
condition <- x[[i]] |>
str_detect(pattern = regex(paste0("\\b", null_terms, "\\b"), ignore_case = TRUE)) |>
any()
}
if (condition) next
out <- append(out, paste0("<b>", tabHeaders[i], ":</b><br>", iconv(x[i], sub = ""), "<br><br>"))
}
return(paste0(out, sep = " "))
}
# str_to_bib
# this function creates a string to be written to a bib file for citations
# x: named list or data frame with names corresponding to bib entries
str_to_bib <- function(x) {
article_key <- gsub(" ", "", paste0(x$citation, x$id, collapse = "_"))
x$title <- x$title |>
iconv("UTF-8", "UTF-8", sub = "")
x$authors_full <- x$authors_full |>
str_replace_all("; ", " and ")
article_meta <- list(author = x$authors_full,
title = x$title,
journal = x$journal,
year = x$year)
out <- paste0("@Article{", article_key, ",\n",
paste0("\t", names(article_meta), " = {",
article_meta, "},", collapse = "\n"),
"\n}")
return(out)
}