-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrlistings_methods.R
375 lines (351 loc) · 10.4 KB
/
rlistings_methods.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
## XXX this historically has been 1, but it actually should be 1.2!!!!!
dflt_courier <- font_spec("Courier", 9, 1)
#' Methods for `listing_df` objects
#'
#' See core documentation in [formatters::formatters-package] for descriptions of these functions.
#'
#' @inheritParams formatters::toString
#' @param x (`listing_df`)\cr the listing.
#' @param ... additional parameters passed to [formatters::toString()].
#'
#' @method print listing_df
#'
#' @export
#' @name listing_methods
print.listing_df <- function(x, widths = NULL, tf_wrap = FALSE, max_width = NULL, fontspec = NULL, col_gap = 3L, ...) {
tryCatch({
cat(
toString(
matrix_form(x, fontspec = fontspec, col_gap = col_gap),
widths = widths,
tf_wrap = tf_wrap,
max_width = max_width,
fontspec = fontspec,
col_gap = col_gap,
...
)
)
}, error = function(e) {
if (nrow(x) == 0) {
print("No observation in the listing object.")
} else {
stop(e)
}
})
invisible(x)
}
#' @exportMethod toString
#' @name listing_methods
#' @aliases toString,listing_df-method
setMethod("toString", "listing_df", function(x, widths = NULL, fontspec = NULL, col_gap = 3L, ...) {
toString(
matrix_form(x, fontspec = fontspec, col_gap = col_gap),
fontspec = fontspec,
col_gap = col_gap,
widths = widths,
...
)
})
## because rle in base is too much of a stickler for being atomic
basic_run_lens <- function(x) {
n <- length(x)
if (n == 0) {
return(integer())
}
y <- x[-1L] != x[-n]
i <- c(which(y), n)
diff(c(0L, i))
}
#' @param df (`listing_df`)\cr the listing.
#' @param colnm (`string`)\cr column name.
#' @param colvec (`vector`)\cr column values based on `colnm`.
#'
#' @rdname vec_nlines
#' @keywords internal
format_colvector <- function(df, colnm, colvec = df[[colnm]]) {
if (missing(colvec) && !(colnm %in% names(df))) {
stop("column ", colnm, " not found")
}
na_str <- obj_na_str(colvec)
if (is.null(na_str) || all(is.na(na_str))) {
na_str <- rep("-", max(1L, length(na_str)))
}
strvec <- vapply(colvec, format_value, "", format = obj_format(colvec), na_str = na_str)
strvec
}
#' Utilities for formatting a listing column
#'
#' For `vec_nlines`, calculate the number of lines each element of a column vector will
#' take to render. For `format_colvector`,
#'
#' @param vec (`vector`)\cr a column vector to be rendered into ASCII.
#' @param max_width (`numeric(1)` or `NULL`)\cr the width to render the column with.
#' @return (`numeric`)\cr a vector of the number of lines element-wise that will be
#' needed to render the elements of `vec` to width `max_width`.
#'
#' @keywords internal
setGeneric("vec_nlines", function(vec, max_width = NULL, fontspec = dflt_courier) standardGeneric("vec_nlines"))
#' @param vec (`vector`)\cr a vector.
#'
#' @rdname vec_nlines
#' @keywords internal
setMethod("vec_nlines", "ANY", function(vec, max_width = NULL, fontspec = dflt_courier) {
if (is.null(max_width)) {
max_width <- floor(0.9 * getOption("width")) # default of base::strwrap
# NB: flooring as it is used as <= (also in base::strwrap)
}
# in formatters for characters
unlist(lapply(format_colvector(colvec = vec), nlines, max_width = max_width, fontspec = fontspec))
})
## setMethod("vec_nlines", "character", function(vec, max_width = NULL) {
## strvec <- wrap_txt(format_colvector(colvec = vec), width = max_width, collapse = "\n")
## mtchs <- gregexpr("\n", strvec, fixed = TRUE)
## 1L + vapply(mtchs, function(vi) sum(vi > 0), 1L)
## })
## setMethod("vec_nlines", "factor", function(vec, max_width = NULL) {
## lvl_nlines <- vec_nlines(levels(vec), max_width = max_width)
## ret <- lvl_nlines[vec]
## ret[is.na(ret)] <- format_value(NA_character
## })
#' Make pagination data frame for a listing
#'
#' @inheritParams formatters::make_row_df
#' @param tt (`listing_df`)\cr the listing to be rendered.
#' @param visible_only (`flag`)\cr ignored, as listings do not have
#' non-visible structural elements.
#'
#' @return a `data.frame` with pagination information.
#'
#' @seealso [formatters::make_row_df()]
#'
#' @examples
#' lsting <- as_listing(mtcars)
#' mf <- matrix_form(lsting)
#'
#' @export
setMethod(
"make_row_df", "listing_df",
function(tt, colwidths = NULL, visible_only = TRUE,
rownum = 0,
indent = 0L,
path = character(),
incontent = FALSE,
repr_ext = 0L,
repr_inds = integer(),
sibpos = NA_integer_,
nsibs = NA_integer_,
fontspec = dflt_courier) {
## assume sortedness by keycols
keycols <- get_keycols(tt)
dispcols <- listing_dispcols(tt)
abs_rownumber <- seq_along(tt[[1]])
if (length(keycols) >= 1) {
runlens <- basic_run_lens(tt[[tail(keycols, 1)]])
} else {
runlens <- rep(1, NROW(tt))
}
sibpos <- unlist(lapply(runlens, seq_len))
nsibs <- rep(runlens, times = runlens)
extents <- rep(1L, nrow(tt))
if (length(colwidths) > 0 && length(colwidths) != length(dispcols)) {
stop(
"Non-null colwidths vector must be the same length as the number of display columns.\n",
"Got: ", length(colwidths), "(", length(dispcols), " disp cols)."
)
}
if (length(colwidths) > 0) {
names(colwidths) <- dispcols
}
## extents is a row-wise vector of extents, for each col, we update
## if that column has any rows wider than the previously recorded extent.
for (col in dispcols) {
## duplicated from matrix_form method, refactor!
col_ext <- vec_nlines(tt[[col]], max_width = colwidths[col], fontspec = fontspec)
extents <- ifelse(col_ext > extents, col_ext, extents)
}
ret <- data.frame(
label = "", name = "",
abs_rownumber = abs_rownumber,
path = I(as.list(rep(NA_character_, NROW(tt)))),
pos_in_siblings = sibpos,
n_siblings = nsibs,
self_extent = extents,
par_extent = 0L,
reprint_inds = I(replicate(NROW(tt), list(integer()))),
node_class = "listing_df",
indent = 0L,
nrowrefs = 0L, ## XXX this doesn't support footnotes
ncellrefs = 0L, ## XXX this doesn't support footnotes
nreflines = 0L, ## XXX this doesn't support footnotes
force_page = FALSE,
page_title = NA_character_,
trailing_sep = NA_character_
)
stopifnot(identical(
names(ret),
names(pagdfrow(
nm = "", lab = "", rnum = 1L, pth = NA_character_, extent = 1L,
rclass = ""
))
))
ret
}
)
## tt$sibpos <- unlist(lapply(
## ## don't support pathing for now
## tt$path <- I(lapply(1:NROW(tt),
## function(i) {
## retpath <- character(2*length(keycols))
## for(j in seq_along(keycols)) {
## retpath[2*j - 1] <- keycols[j]
## retpath[2*j] <- tt[i, keycols[j], drop = TRUE]
## }
## retpath
## }))
## spl <- split(tt, tt[keycols])
## spl <- spl[vapply(spl, function(y) NROW(y) > 0, NA)]
## dfs <- lapply(spl, function(df) {
## df <- df[order(df$abs_rownumber),]
## ndf <- NROW(df)
## lapply(1:ndf, function(i) {
## rw <- df[i,]
## stopifnot(nrow(rw) == 1)
## pagdfrow(nm = "",
## lab = "",
## rnum = rw$abs_rownumber,
## pth = NA_character_,
## sibpos = i,
## nsibs = ndf,
## extent = 1L,
## rclass = "listing_df",
## repind = integer())
## })
## })
## ret <- do.call(rbind, unlist(dfs, recursive = FALSE))
## ret <- ret[order(ret$abs_rownumber),]
## ret
## })
#' @inheritParams base::Extract
#' @param x (`listing_df`)\cr the listing.
#' @param i (`any`)\cr object passed to base `[` methods.
#' @param j (`any`)\cr object passed to base `[` methods.
#'
#' @export
#' @aliases [,listing_df-method
#' @rdname listing_methods
setMethod(
"[", "listing_df",
function(x, i, j, drop = FALSE) {
xattr <- attributes(x)
xattr$names <- xattr$names[j]
res <- NextMethod()
if (!drop) {
attributes(res) <- xattr
}
res
}
)
#' @rdname listing_methods
#' @param obj (`listing_df`)\cr the listing.
#'
#' @return
#' * Accessor methods return the value of the aspect of `obj`.
#' * Setter methods return `obj` with the relevant element of the listing updated.
#'
#' @examples
#' lsting <- as_listing(mtcars)
#' main_title(lsting) <- "Hi there"
#'
#' main_title(lsting)
#'
#' @export
setMethod(
"main_title", "listing_df",
function(obj) attr(obj, "main_title") %||% character()
)
#' @rdname listing_methods
#' @export
setMethod(
"subtitles", "listing_df",
function(obj) attr(obj, "subtitles") %||% character()
)
#' @rdname listing_methods
#' @export
setMethod(
"main_footer", "listing_df",
function(obj) attr(obj, "main_footer") %||% character()
)
#' @rdname listing_methods
#' @export
setMethod(
"prov_footer", "listing_df",
function(obj) attr(obj, "prov_footer") %||% character()
)
.chk_value <- function(val, fname, len_one = FALSE, null_ok = TRUE) {
if (null_ok && is.null(val)) {
return(TRUE)
}
if (!is.character(val)) {
stop("value for ", fname, " must be a character, got ",
"object of class: ", paste(class(val), collapse = ","),
call. = FALSE
)
}
if (len_one && length(val) > 1) {
stop(
"value for ", fname, " must be length <= 1, got ",
"vector of length ", length(val)
)
}
TRUE
}
#' @rdname listing_methods
#' @export
setMethod(
"main_title<-", "listing_df",
function(obj, value) {
## length 1 restriction is to match rtables behavior
## which currently enforces this (though incompletely)
.chk_value(value, "main_title", len_one = TRUE)
attr(obj, "main_title") <- value
obj
}
)
#' @rdname listing_methods
#' @export
setMethod(
"subtitles<-", "listing_df",
function(obj, value) {
.chk_value(value, "subtitles")
attr(obj, "subtitles") <- value
obj
}
)
#' @rdname listing_methods
#' @export
setMethod(
"main_footer<-", "listing_df",
function(obj, value) {
.chk_value(value, "main_footer")
attr(obj, "main_footer") <- value
obj
}
)
#' @rdname listing_methods
#' @export
setMethod(
"prov_footer<-", "listing_df",
function(obj, value) {
.chk_value(value, "prov_footer")
attr(obj, "prov_footer") <- value
obj
}
)
#' @rdname listing_methods
#' @export
setMethod(
"num_rep_cols", "listing_df",
function(obj) {
length(get_keycols(obj))
}
)