-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
html_paged.R
248 lines (213 loc) · 6.58 KB
/
html_paged.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
# paged_table_type_sum and paged_table_obj_sum should be replaced
# by tibble::type_sum once tibble supports R 3.0.0.
paged_table_type_sum <- function(x) {
# support vctrs-type vectors if the package is installed
if (xfun::loadable("vctrs") && vctrs::vec_is(x)) return(vctrs::vec_ptype_abbr(x))
type_sum <- function(x)
{
format_sum <- switch(
class(x)[[1]], ordered = "ord", factor = "fctr", POSIXt = "dttm",
difftime = "time", Date = "date", data.frame = class(x)[[1]],
tbl_df = "tibble", NULL
)
if (!is.null(format_sum)) {
format_sum
} else if (!is.object(x)) {
switch(typeof(x),
logical = "lgl",
integer = "int",
double = "dbl",
character = "chr",
complex = "cplx",
closure = "fun",
environment = "env",
typeof(x)
)
} else if (!isS4(x)) {
paste0("S3: ", class(x)[[1]])
} else {
paste0("S4: ", methods::is(x)[[1]])
}
}
type_sum(x)
}
paged_table_obj_sum <- function(x) {
big_mark <- function(x, ...) {
mark <- if (identical(getOption("OutDec"), ",")) "." else ","
formatC(x, big.mark = mark, ...)
}
dim_desc <- function(x) {
dim <- dim(x) %||% length(x)
format_dim <- vapply(dim, big_mark, character(1))
format_dim[is.na(dim)] <- "??"
paste0(format_dim, collapse = " \u00d7 ")
}
is_atomic <- function(x) {
is.atomic(x) && !is.null(x)
}
is_vector <- function(x) {
is_atomic(x) || is.list(x)
}
paged_table_is_vector_s3 <- function(x) {
switch(class(x)[[1]],
ordered = TRUE,
factor = TRUE,
Date = TRUE,
POSIXct = TRUE,
difftime = TRUE,
data.frame = TRUE,
!is.object(x) && is_vector(x))
}
size_sum <- function(x) {
if (!paged_table_is_vector_s3(x)) return("")
paste0(" [", dim_desc(x), "]" )
}
obj_sum.default <- function(x) {
paste0(paged_table_type_sum(x), size_sum(x))
}
switch(class(x)[[1]],
POSIXlt = rep("POSIXlt", length(x)),
list = vapply(x, obj_sum.default, character(1L)),
paste0(paged_table_type_sum(x), size_sum(x))
)
}
paged_table_option <- function(option, options, default = NULL) {
if (is.null(options))
options <- list()
if (!is.null(options[[option]]))
options[[option]]
else if (is.null(knitr::opts_current$get(option)))
getOption(option, default)
else
knitr::opts_current$get(option)
}
#' @import methods
paged_table_html <- function(x, options = NULL) {
addRowNames = paged_table_option("rownames.print", options)
addRowNames <- if (is.null(addRowNames)) (.row_names_info(x, type = 1) > 0) else addRowNames
maxPrint <- paged_table_option("max.print", options, 1000)
if ("tbl_sql" %in% class(x)) {
maxPrint <- paged_table_option("sql.max.print", options, 1000)
}
# hard stop at 10K items to print to prevent pandoc from failing
maxPrint <- if (maxPrint > 10000) 10000 else maxPrint
data <- as.data.frame(utils::head(x, maxPrint))
data <- if (is.null(data)) as.data.frame(list()) else data
columnNames <- names(data)
columnSequence <- seq_len(ncol(data))
columns <- lapply(
columnSequence,
function(columnIdx) {
column <- data[[columnIdx]]
baseType <- class(column)[[1]]
tibbleType <- paged_table_type_sum(column)
list(
label = if (!is.null(columnNames)) columnNames[[columnIdx]] else "",
name = columnIdx,
type = tibbleType,
align = if (baseType == "character" || baseType == "factor") "left" else "right"
)
}
)
names(data) <- as.character(columnSequence)
if (addRowNames) {
columns <- c(
list(
list(
label = "",
name = "_rn_",
type = "",
align = "left"
)
),
columns
)
data$`_rn_` <- rownames(data)
}
columns <- unname(columns)
is_list <- vapply(data, is.list, logical(1))
data[is_list] <- lapply(data[is_list], function(x) {
summary <- paged_table_obj_sum(x)
paste0("<", summary, ">")
})
data <- as.data.frame(
lapply(
data,
function(y) {
# escape NAs from character columns
if (typeof(y) == "character") {
y[y == "NA"] <- "__NA__"
}
y <- encodeString(format(y, digits = getOption("digits")))
# trim spaces
gsub("^\\s+|\\s+$", "", y)
}
),
stringsAsFactors = FALSE,
optional = TRUE)
rowCount <- paged_table_option("rows.print", options, 10)
pagedTableOptions <- list(
columns = list(
min = paged_table_option("cols.min.print", options),
max = paged_table_option("cols.print", options, 10)
),
rows = list(
min = rowCount,
max = rowCount
),
pages = paged_table_option("pages.print", options)
)
pagedData <- list(
columns = columns,
data = if (length(data) == 0) list() else data,
options = pagedTableOptions
)
paste(
"<div data-pagedtable=\"false\">",
" <script data-pagedtable-source type=\"application/json\">",
jsonlite::toJSON(pagedData),
" </script>",
"</div>",
sep = "\n"
)
}
#' Create a table in HTML with support for paging rows and columns
#'
#' @param x a data frame to be rendered as a paged table.
#' @param options options for printing the paged table. See details for specifics.
#'
#' @details
#' Below are the recognized table pagination options.
#'
#' \tabular{rll}{
#' Option \tab Description \tab Default \cr
#' \code{max.print} \tab The number of rows to print. \tab 1000 \cr
#' \code{sql.max.print} \tab The number of rows to print from a SQL data table. \tab 1000 \cr
#' \code{rows.print} \tab The number of rows to display. \tab 10 \cr
#' \code{cols.print} \tab The number of columns to display. \tab 10 \cr
#' \code{cols.min.print} \tab The minimum number of columns to display. \tab - \cr
#' \code{pages.print} \tab The number of pages to display under page navigation. \tab - \cr
#' \code{paged.print} \tab When set to FALSE turns off paged tables. \tab TRUE \cr
#' \code{rownames.print} \tab When set to FALSE turns off row names. \tab TRUE
#' }
#'
#' \bold{Note:} There is a hard cap of 10,000 rows to ensure that pandoc will not
#' fail when rendering the document.
#'
#' @export
paged_table <- function(x, options = NULL) {
if (!is.data.frame(x)) {
stop("Only data frames can be used to create a paged_table.")
}
class(x) <- c("paged_df", "data.frame")
attr(x, "options") <- options
x
}
print.paged_df <- function(x) {
knitr::asis_output(
paged_table_html(x, options = attr(x, "options")),
meta = list(
dependencies = html_dependency_pagedtable()
)
)
}