/
utils.R
192 lines (114 loc) · 4.69 KB
/
utils.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
#' @importFrom stringi stri_extract
extract_packages <- function(text){
# extracts package names from text file
# finds all library/require(),
# and explicit package::function() calls
# drop comments
text <- text[!grepl("[ ]*(#).*$", text)]
# grab packages (loading)
loaded <- text[grepl("(library|require)\\(([a-zA-Z0-9]*)\\)",
text)]
pkgs_load <- gsub(".*(library|require)\\(([a-zA-Z0-9]*)\\).*",
"\\2",
loaded)
pkgs_colon <- stringi::stri_extract(text, regex = "([[:alnum:]]+)(?=::)")
# grab packages (explicit :: notation)
pkgs <- c(pkgs_load,
pkgs_colon[!is.na(pkgs_colon)])
# gsub(pattern = "(\w+)(?=::)",
# # gsub(pattern = "(.*[[:graph:][:space:]]){0,1}([a-zA-Z0-9]*)(::)(.*)",
# x = text[grepl(pattern = "([a-zA-Z])+(::){1}", x = text)],
# replacement = "\\2",
# perl = TRUE)
# )
return(unique(pkgs))
}
check_if_r <- function(path){
# https://github.com/juba/questionr/blob/master/R/utils.R
is_r <- grepl(pattern = "[.]r$", x = path, ignore.case = TRUE)
return(is_r)
}
check_if_rmd <- function(path){
# https://github.com/juba/questionr/blob/master/R/utils.R
is_rmd <- grepl(pattern = "[.]rmd$", x = path, ignore.case = TRUE) ||
grepl(pattern = "[.]rmarkdown$", x = path, ignore.case = TRUE)
return(is_rmd)
}
#' @importFrom purrr map2
make_bibtex_list <- function(libs_bibtex){
# id indices of each entry
id_entries <- function(libs_bibtex){
entry_start <- grep(pattern = "^@[[:alnum:]]+[{][,]",
x = libs_bibtex)
entry_end <- grep(pattern = "^[}]$",
x = libs_bibtex)
entries <- cbind(entry_start, entry_end)
n_entries <- nrow(entries)
entry_details <- list(entries = entries,
n_entries = n_entries)
return(entry_details)
}
entries <- lapply(libs_bibtex, id_entries)
res_libs <- vector(mode = "list",
length = sum(sapply(entries,
`[[`,
"n_entries")))
names(res_libs) <- unlist(
purrr::map2(
names(sapply(entries, `[[`, "n_entries")),
sapply(entries, `[[`, "n_entries"),
rep))
# fill res libs based on number of items in entries
res_libs_idx <- 1
for(j in seq_along(entries)){
n_entries <- entries[[j]][["n_entries"]]
# fill list items with each occurence of an entry
res_libs[res_libs_idx:(res_libs_idx+n_entries-1)] <-
purrr::map2(entries[[j]][['entries']][,'entry_start'],
entries[[j]][['entries']][,'entry_end'],
function(x,y){
c(libs_bibtex[[j]][x:y])
})
res_libs_idx <- sum(sapply(res_libs, Negate(is.null))) + 1
}
return(res_libs)
}
make_pckgs_vector <- function(libs, textformat = TRUE){
if(textformat){
libs_short <- unlist(lib_print(libs,
textformat = TRUE,
silent = TRUE))
# grab single and repeated names
names(libs_short) <- rep.int(names(sapply(lib_print(libs,
textformat = TRUE,
silent = TRUE),
length)),
times = sapply(lib_print(libs,
textformat = TRUE,
silent = TRUE),
length))
} else if(isFALSE(textformat)){
# makes a list with one entry for each citation
# packages with multiple citations get on entry for each
# provided citation
libs_short <- lapply(
make_bibtex_list(
lib_print(libs,
textformat = FALSE,
silent = TRUE)),
paste,
collapse = "\n"
)
libs_short <- unlist(libs_short)
}
return(libs_short)
}
check_if_libs_is_df <- function(libs){
# check for object type and adjust call to citation
if(is.data.frame(libs)){
citations <- lapply(libs$Package, citation)
} else {
citations <- lapply(libs, citation)
}
return(citations)
}