| 1 |
# paged_table_type_sum and paged_table_obj_sum should be replaced |
| 2 |
# by tibble::type_sum once tibble supports R 3.0.0. |
| 3 |
paged_table_type_sum <- function(x) { |
| 4 |
# support vctrs-type vectors if the package is installed |
| 5 |
if (xfun::loadable("vctrs") && vctrs::vec_is(x)) return(vctrs::vec_ptype_abbr(x)) |
| 6 |
type_sum <- function(x) |
| 7 |
{ |
| 8 |
format_sum <- switch( |
| 9 |
class(x)[[1]], ordered = "ord", factor = "fctr", POSIXt = "dttm", |
| 10 |
difftime = "time", Date = "date", data.frame = class(x)[[1]], |
| 11 |
tbl_df = "tibble", NULL |
| 12 |
) |
| 13 |
if (!is.null(format_sum)) { |
| 14 |
format_sum |
| 15 |
} else if (!is.object(x)) { |
| 16 |
switch(typeof(x), |
| 17 |
logical = "lgl", |
| 18 |
integer = "int", |
| 19 |
double = "dbl", |
| 20 |
character = "chr", |
| 21 |
complex = "cplx", |
| 22 |
closure = "fun", |
| 23 |
environment = "env", |
| 24 |
typeof(x) |
| 25 |
) |
| 26 |
} else if (!isS4(x)) { |
| 27 |
paste0("S3: ", class(x)[[1]]) |
| 28 |
} else { |
| 29 |
paste0("S4: ", methods::is(x)[[1]]) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
type_sum(x) |
| 34 |
} |
| 35 |
|
| 36 |
paged_table_obj_sum <- function(x) { |
| 37 |
big_mark <- function(x, ...) { |
| 38 |
mark <- if (identical(getOption("OutDec"), ",")) "." else "," |
| 39 |
formatC(x, big.mark = mark, ...) |
| 40 |
} |
| 41 |
|
| 42 |
dim_desc <- function(x) { |
| 43 |
dim <- dim(x) %||% length(x) |
| 44 |
format_dim <- vapply(dim, big_mark, character(1)) |
| 45 |
format_dim[is.na(dim)] <- "??" |
| 46 |
paste0(format_dim, collapse = " \u00d7 ") |
| 47 |
} |
| 48 |
|
| 49 |
is_atomic <- function(x) { |
| 50 |
is.atomic(x) && !is.null(x) |
| 51 |
} |
| 52 |
|
| 53 |
is_vector <- function(x) { |
| 54 |
is_atomic(x) || is.list(x) |
| 55 |
} |
| 56 |
|
| 57 |
paged_table_is_vector_s3 <- function(x) { |
| 58 |
switch(class(x)[[1]], |
| 59 |
ordered = TRUE, |
| 60 |
factor = TRUE, |
| 61 |
Date = TRUE, |
| 62 |
POSIXct = TRUE, |
| 63 |
difftime = TRUE, |
| 64 |
data.frame = TRUE, |
| 65 |
!is.object(x) && is_vector(x)) |
| 66 |
} |
| 67 |
|
| 68 |
size_sum <- function(x) { |
| 69 |
if (!paged_table_is_vector_s3(x)) return("") |
| 70 |
|
| 71 |
paste0(" [", dim_desc(x), "]" ) |
| 72 |
} |
| 73 |
|
| 74 |
obj_sum.default <- function(x) { |
| 75 |
paste0(paged_table_type_sum(x), size_sum(x)) |
| 76 |
} |
| 77 |
|
| 78 |
switch(class(x)[[1]], |
| 79 |
POSIXlt = rep("POSIXlt", length(x)), |
| 80 |
list = vapply(x, obj_sum.default, character(1L)), |
| 81 |
paste0(paged_table_type_sum(x), size_sum(x)) |
| 82 |
) |
| 83 |
} |
| 84 |
|
| 85 |
paged_table_option <- function(option, options, default = NULL) { |
| 86 |
if (is.null(options)) |
| 87 |
options <- list() |
| 88 |
if (!is.null(options[[option]])) |
| 89 |
options[[option]] |
| 90 |
else if (is.null(knitr::opts_current$get(option))) |
| 91 |
getOption(option, default) |
| 92 |
else |
| 93 |
knitr::opts_current$get(option) |
| 94 |
} |
| 95 |
|
| 96 |
#' @import methods |
| 97 |
paged_table_html <- function(x, options = NULL) { |
| 98 |
addRowNames = paged_table_option("rownames.print", options) |
| 99 |
addRowNames <- if (is.null(addRowNames)) (.row_names_info(x, type = 1) > 0) else addRowNames |
| 100 |
|
| 101 |
maxPrint <- paged_table_option("max.print", options, 1000) |
| 102 |
|
| 103 |
if ("tbl_sql" %in% class(x)) { |
| 104 |
maxPrint <- paged_table_option("sql.max.print", options, 1000) |
| 105 |
} |
| 106 |
|
| 107 |
# hard stop at 10K items to print to prevent pandoc from failing |
| 108 |
maxPrint <- if (maxPrint > 10000) 10000 else maxPrint |
| 109 |
|
| 110 |
data <- as.data.frame(utils::head(x, maxPrint)) |
| 111 |
|
| 112 |
data <- if (is.null(data)) as.data.frame(list()) else data |
| 113 |
|
| 114 |
columnNames <- names(data) |
| 115 |
columnSequence <- seq_len(ncol(data)) |
| 116 |
|
| 117 |
columns <- lapply( |
| 118 |
columnSequence, |
| 119 |
function(columnIdx) { |
| 120 |
column <- data[[columnIdx]] |
| 121 |
baseType <- class(column)[[1]] |
| 122 |
tibbleType <- paged_table_type_sum(column) |
| 123 |
|
| 124 |
list( |
| 125 |
label = if (!is.null(columnNames)) columnNames[[columnIdx]] else "", |
| 126 |
name = columnIdx, |
| 127 |
type = tibbleType, |
| 128 |
align = if (baseType == "character" || baseType == "factor") "left" else "right" |
| 129 |
) |
| 130 |
} |
| 131 |
) |
| 132 |
|
| 133 |
names(data) <- as.character(columnSequence) |
| 134 |
|
| 135 |
if (addRowNames) { |
| 136 |
columns <- c( |
| 137 |
list( |
| 138 |
list( |
| 139 |
label = "", |
| 140 |
name = "_rn_", |
| 141 |
type = "", |
| 142 |
align = "left" |
| 143 |
) |
| 144 |
), |
| 145 |
columns |
| 146 |
) |
| 147 |
|
| 148 |
data$`_rn_` <- rownames(data) |
| 149 |
} |
| 150 |
|
| 151 |
columns <- unname(columns) |
| 152 |
|
| 153 |
is_list <- vapply(data, is.list, logical(1)) |
| 154 |
data[is_list] <- lapply(data[is_list], function(x) { |
| 155 |
summary <- paged_table_obj_sum(x) |
| 156 |
paste0("<", summary, ">") |
| 157 |
}) |
| 158 |
|
| 159 |
data <- as.data.frame( |
| 160 |
lapply( |
| 161 |
data, |
| 162 |
function(y) { |
| 163 |
# escape NAs from character columns |
| 164 |
if (typeof(y) == "character") { |
| 165 |
y[y == "NA"] <- "__NA__" |
| 166 |
} |
| 167 |
|
| 168 |
y <- encodeString(format(y, digits = getOption("digits"))) |
| 169 |
|
| 170 |
# trim spaces |
| 171 |
gsub("^\\s+|\\s+$", "", y) |
| 172 |
} |
| 173 |
), |
| 174 |
stringsAsFactors = FALSE, |
| 175 |
optional = TRUE) |
| 176 |
|
| 177 |
rowCount <- paged_table_option("rows.print", options, 10) |
| 178 |
|
| 179 |
pagedTableOptions <- list( |
| 180 |
columns = list( |
| 181 |
min = paged_table_option("cols.min.print", options), |
| 182 |
max = paged_table_option("cols.print", options, 10) |
| 183 |
), |
| 184 |
rows = list( |
| 185 |
min = rowCount, |
| 186 |
max = rowCount |
| 187 |
), |
| 188 |
pages = paged_table_option("pages.print", options) |
| 189 |
) |
| 190 |
|
| 191 |
pagedData <- list( |
| 192 |
columns = columns, |
| 193 |
data = if (length(data) == 0) list() else data, |
| 194 |
options = pagedTableOptions |
| 195 |
) |
| 196 |
|
| 197 |
paste( |
| 198 |
"<div data-pagedtable=\"false\">", |
| 199 |
" <script data-pagedtable-source type=\"application/json\">", |
| 200 |
jsonlite::toJSON(pagedData), |
| 201 |
" </script>", |
| 202 |
"</div>", |
| 203 |
sep = "\n" |
| 204 |
) |
| 205 |
} |
| 206 |
|
| 207 |
#' Create a table in HTML with support for paging rows and columns |
| 208 |
#' |
| 209 |
#' @param x a data frame to be rendered as a paged table. |
| 210 |
#' @param options options for printing the paged table. See details for specifics. |
| 211 |
#' |
| 212 |
#' @details |
| 213 |
#' Below are the recognized table pagination options. |
| 214 |
#' |
| 215 |
#' \tabular{rll}{ |
| 216 |
#' Option \tab Description \tab Default \cr |
| 217 |
#' \code{max.print} \tab The number of rows to print. \tab 1000 \cr |
| 218 |
#' \code{sql.max.print} \tab The number of rows to print from a SQL data table. \tab 1000 \cr |
| 219 |
#' \code{rows.print} \tab The number of rows to display. \tab 10 \cr |
| 220 |
#' \code{cols.print} \tab The number of columns to display. \tab 10 \cr |
| 221 |
#' \code{cols.min.print} \tab The minimum number of columns to display. \tab - \cr |
| 222 |
#' \code{pages.print} \tab The number of pages to display under page navigation. \tab - \cr |
| 223 |
#' \code{paged.print} \tab When set to FALSE turns off paged tables. \tab TRUE \cr |
| 224 |
#' \code{rownames.print} \tab When set to FALSE turns off row names. \tab TRUE |
| 225 |
#' } |
| 226 |
#' |
| 227 |
#' \bold{Note:} There is a hard cap of 10,000 rows to ensure that pandoc will not |
| 228 |
#' fail when rendering the document. |
| 229 |
#' |
| 230 |
#' @export |
| 231 |
paged_table <- function(x, options = NULL) { |
| 232 |
if (!is.data.frame(x)) { |
| 233 |
stop("Only data frames can be used to create a paged_table.") |
| 234 |
} |
| 235 |
|
| 236 |
class(x) <- c("paged_df", "data.frame") |
| 237 |
attr(x, "options") <- options |
| 238 |
x |
| 239 |
} |
| 240 |
|
| 241 |
#' @export |
| 242 |
print.paged_df <- function(x, ...) { |
| 243 |
knitr::asis_output( |
| 244 |
paged_table_html(x, options = attr(x, "options")), |
| 245 |
meta = list( |
| 246 |
dependencies = html_dependency_pagedtable() |
| 247 |
) |
| 248 |
) |
| 249 |
} |