-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_namer.R
283 lines (237 loc) · 8.44 KB
/
utils_namer.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
###############################
#### Namer Utilities ####
###############################
# Utility from namer R-package https://github.com/lockedata/namer
# changes were made to allow parsing chunks for Rmd files as well as Rnw files
#---- quote_label ----
# function from knitr r-package
# https://github.com/yihui/knitr/blob/2b3e617a700f6d236e22873cfff6cbc3568df568/R/parser.R#L148
# quote the chunk label if necessaryme issues
# Adapted to solve so
quote_label = function(x) {
x <- gsub('^\\s*,?', '', x)
if (grepl('^\\s*[^\'"](,|\\s*$)', x)) {
if(grepl('^\\s*[^\'"],\\s*[^=]*(,|\\s*$)', x)){
# "r, chucnk_name" ---> "'r chucnk_name'"
x <- gsub('^\\s*([^\'"]),(\\s*[^=]*)(,|\\s*$)', "'\\1,\\2'\\3", x)
} else {
# <<a,b=1>>= ---> <<'a',b=1>>=
x = gsub('^\\s*([^\'"])(,|\\s*$)', "'\\1'\\2", x)
}
} else if (grepl('^\\s*[^\'"](,|[^=]*(,|\\s*$))', x)) {
# <<abc,b=1>>= ---> <<'abc',b=1>>=
x <- gsub('^\\s*([^\'"][^=]*)(,|\\s*$)', "'\\1'\\2", x)
}
x
}
#---- get_chunk_info ----
# function from namer r-package
# https://github.com/lockedata/namer/blob/master/R/utils.R#L72
# adapted to consider rmd and rnw
#' Get chunk info
#'
#' helper to create a data.frame of chunk info
#'
#' @param lines a character vector with the text lines of the file
#' @param info_patterns a list with the regex pattern according to file
#' extension, returned by get_extension_patterns() function
#'
#' @return a tibble with \itemize{
#' \item{language} of the chunk
#' \item{name} of the chunk
#' \item{options} of the chunk
#' \item{starts} the line number of the chunk header
#' \item{ends} the line number of the chunk end
#' \item{index} integer index to identify the chunk
#' }
#' Note that in case of "rnw" extension the language is always NA. NULL is
#' returned if no chunk was available.
#' @noRd
#'
#' @examples
#' # rmd
#' lines <- readLines("tests/testthat/test_files/example_1_rmd.txt")
#' info_patterns <- get_extension_patterns(extension = "rmd")
#' get_chunk_info(lines, info_patterns)
#'
#' # rnw
#' lines <- readLines("tests/testthat/test_files/example_1_rnw.txt")
#' info_patterns <- get_extension_patterns(extension = "rnw")
#' get_chunk_info(lines, info_patterns)
#'
get_chunk_info <- function(lines, info_patterns){
# find which lines are chunk starts and chuck ends
chunks_range <- get_chunk_range(lines, info_patterns)
# null if no chunks
if(length(chunks_range$starts) == 0){
return(NULL)
}
# parse these chunk headers
res <- purrr::map_df(chunks_range$starts,
digest_chunk_header,
lines,
info_patterns)
# return also chunk start/end line indexes
res$starts <- chunks_range$starts
res$ends <- chunks_range$ends
res$index <- seq(length.out = nrow(res))
return(res)
}
#---- parse_chunk_header ----
# function from namer r-package
# https://github.com/lockedata/namer/blob/master/R/utils.R#L35
#' Parse chunk header
#'
#' from a chunk header to a tibble with language, name, option, option values
#'
#' @param chunk_header a string with the chunk header
#' @param info_patterns a list with the regex pattern according to file
#' extension, returned by get_extension_patterns() function
#'
#' @return a tibble with \itemize{
#' \item{language} of the chunk
#' \item{name} of the chunk
#' \item{options} of the chunk
#' }
#' Note that in case of "rnw" extension the language is always NA
#' @noRd
#'
#' @examples
#' # rmd
#' chunk_header_rmd <- "```{r setup, include=FALSE}"
#' info_patterns <- get_extension_patterns(extension = "rmd")
#' parse_chunk_header(chunk_header_rmd, info_patterns)
#'
#' # rnw
#' chunk_header_rnw <- "<<setup, include=FALSE>>="
#' info_patterns <- get_extension_patterns(extension = "rnw")
#' parse_chunk_header(chunk_header_rnw, info_patterns)
#'
parse_chunk_header <- function(chunk_header, info_patterns){
# remove boundaries
chunk_header <- gsub(info_patterns$chunk_header_start, "", chunk_header)
chunk_header <- gsub(info_patterns$chunk_header_end, "", chunk_header)
# parse each part
transform_params(chunk_header, extension = info_patterns$extension)
}
#---- digest_chunk_header ----
# function from namer r-package
# https://github.com/lockedata/namer/blob/master/R/utils.R#L45
# adapted to consider rmd and rnw
#' Digest chunk header
#'
#'
#' @param chunk_header_index integer indicating the line index of the chunch
#' header
#' @param lines a character vector with the text lines of the file
#' @param info_patterns a list with the regex pattern according to file
#' extension, returned by get_extension_patterns() function
#'
#' @return a tibble with \itemize{
#' \item{language} of the chunk
#' \item{name} of the chunk
#' \item{options} of the chunk
#' }
#' Note that in case of "rnw" extension the language is always NA
#' @noRd
#'
#' @examples
#' # rmd
#' lines <- readLines("tests/testthat/test_files/example_1_rmd.txt")
#' info_patterns <- get_extension_patterns(extension = "rmd")
#' chunk_header_index <- which(grepl(info_patterns$chunk_header_start, lines))[1]
#' digest_chunk_header(chunk_header_index, lines, info_patterns)
#'
#' # rnw
#' lines <- readLines("tests/testthat/test_files/example_1_rnw.txt")
#' info_patterns <- get_extension_patterns(extension = "rnw")
#' chunk_header_index <- which(grepl(info_patterns$chunk_header_start, lines))[1]
#' digest_chunk_header(chunk_header_index, lines, info_patterns)
#'
digest_chunk_header <- function(chunk_header_index,
lines,
info_patterns){
# parse the chunk header
chunk_info <- parse_chunk_header(
chunk_header = lines[chunk_header_index],
info_patterns = info_patterns)
return(chunk_info)
}
#---- transform_params ----
# function from namer r-package
# https://github.com/lockedata/namer/blob/master/R/utils.R#L3
# adapted to consider rmd and rnw
#' Transform Parameters
#'
#' not elegant, given a part of a header, transform it into the row of a tibble
#'
#' @param params a string indicating the content of a chunk header
#' @param extension a indicating the extension of the file ("rmd" or "rnw")
#'
#' @return a tibble with \itemize{
#' \item{language} of the chunk
#' \item{name} of the chunk
#' \item{options} of the chunk
#' }
#' Note that in case of "rnw" extension the language is always NA
#' @noRd
#'
#' @examples
#' # rmd
#' params_rmd <- "r setup, include=FALSE"
#' transform_params(params_rmd, extension = "rmd")
#'
#' # rnw
#' params_rnw <- "setup, include=FALSE"
#' transform_params(params_rnw, extension = "rnw")
#'
transform_params <- function(params, extension){
params_string <- try(eval(parse(text = paste('alist(', quote_label(params), ')'))),
silent = TRUE)
if(inherits(params_string, "try-error")){
params <- sub(" ", ", ", params)
params_string <- eval(parse(text = paste('alist(', quote_label(params), ')')))
}
if (extension == "rmd"){
res <- parse_label_rmd(params_string, params)
} else if (extension == "rnw") {
res <- parse_label_rnw(params_string, params)
}
return(res)
}
#---- parse_label_rmd ----
# function from namer r-package
# https://github.com/lockedata/namer/blob/master/R/utils.R#L20
parse_label_rmd <- function(label, params){
if(length(label) < 1 || (!is.null(names(label)[1]) && names(label)[1]!= "")){
# chunk with no language e no name only options
language <- NA
name_chunk <- NA
options <- params
} else {
language_name <- sub(",?\\s+", "\\/", label[[1]])
language_name <- unlist(strsplit(language_name, "\\/"))
language <- trimws(language_name[1])
name_chunk <- ifelse(length(language_name) == 1L, NA, trimws(language_name[2]))
options <- sub(label[[1]], "", params)
}
tibble::tibble(language = language,
name = name_chunk,
options = options)
}
#---- parse_label_rnw ----
parse_label_rnw <- function(label, params){
if(length(label) < 1 || (!is.null(names(label)[1]) && names(label)[1]!= "")){
# empty chunk (e.g., <<>>=) or
# chunk with arguments but not name (e.g., <<eval = TRUE>>=)
name_chunk <- NA
options <- params
} else { # chunk with name
name_chunk <- trimws(label[1])
options <- sub(label[[1]], "", params)
}
tibble::tibble(language = NA,
name = name_chunk,
options = options)
}
#----