Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load chars on package load #160

Merged
merged 6 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ Collate:
'repr_spatial.r'
'repr_ts.r'
'repr_vega.r'
'zzz_onload.r'
RoxygenNote: 7.2.3
2 changes: 1 addition & 1 deletion R/options.r
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class_defaults <- list(
#' @export
repr_option_defaults <- c(plot_defaults, class_defaults)

.onLoad <- function(libname = NULL, pkgname = NULL) {
onload_options <- function() {
for (opt_name in names(repr_option_defaults)) {
if (is.null(getOption(opt_name)))
do.call(options, repr_option_defaults[opt_name]) # single []: name stays
Expand Down
37 changes: 19 additions & 18 deletions R/repr_matrix_df.r
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ NULL
# See https://github.com/IRkernel/repr/issues/28#issuecomment-208574856
#' @importFrom utils capture.output
.char_fallback <- function(char, default) {
real_len <- nchar(char)
r_len <- nchar(capture.output(cat(char)))
if (real_len == r_len) char else default
real_len <- nchar(char)
r_len <- nchar(capture.output(cat(char)))
if (real_len == r_len) char else default
}
ellip_h <- .char_fallback('\u22EF', '...')
ellip_v <- .char_fallback('\u22EE', '...')
ellip_d <- .char_fallback('\u22F1', '')
times_s <- .char_fallback('\u00D7', 'x')

# These are used for factor, so make sure they are unique
ellipses <- unique(c(ellip_h, ellip_v, ellip_d))
chars <- new.env()
onload_chars <- function() {
chars$ellip_h <- .char_fallback('\u22EF', '...')
chars$ellip_v <- .char_fallback('\u22EE', '...')
chars$ellip_d <- .char_fallback('\u22F1', '')
chars$times_s <- .char_fallback('\u00D7', 'x')
}

arr_partition <- function(a, rows, cols) {
stopifnot(rows >= 2L, cols >= 2L)
Expand Down Expand Up @@ -81,21 +82,21 @@ arr_part_format <- function(part) {
arr_parts_combine <- function(parts, rownms, colnms) {
omit <- attr(parts, 'omit')
mat <- switch(omit,
rows = rbind(parts$upper, ellip_v, parts$lower, deparse.level = 0L),
cols = cbind(parts$left, ellip_h, parts$right, deparse.level = 0L),
rows = rbind(parts$upper, chars$ellip_v, parts$lower, deparse.level = 0L),
cols = cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L),
none = parts$full,
both = rbind(
cbind(parts$ul, ellip_h, parts$ur, deparse.level = 0L),
c(rep(ellip_v, ncol(parts$ul)), ellip_d, rep(ellip_v, ncol(parts$ur))),
cbind(parts$ll, ellip_h, parts$lr, deparse.level = 0L)))
cbind(parts$ul, chars$ellip_h, parts$ur, deparse.level = 0L),
c(rep(chars$ellip_v, ncol(parts$ul)), chars$ellip_d, rep(chars$ellip_v, ncol(parts$ur))),
cbind(parts$ll, chars$ellip_h, parts$lr, deparse.level = 0L)))

# If there were no dimnames before, as is often true for matrices, don't assign them.
if (omit %in% c('rows', 'both') && !is.null(rownms)) {
# everything except ellip_v is to fix rownames for tbls, which explicitly set them to 1:n when subsetting
rownames(mat) <- c(head(rownms, nrow(parts[[1]])), ellip_v, tail(rownms, nrow(parts[[2]])))
rownames(mat) <- c(head(rownms, nrow(parts[[1]])), chars$ellip_v, tail(rownms, nrow(parts[[2]])))
}
if (omit %in% c('cols', 'both') && !is.null(colnms)) {
colnames(mat)[[ncol(parts[[1]]) + 1L]] <- ellip_h
colnames(mat)[[ncol(parts[[1]]) + 1L]] <- chars$ellip_h
}

mat
Expand Down Expand Up @@ -195,7 +196,7 @@ repr_matrix_generic <- function(

body <- sprintf(body_wrap, paste(rows, collapse = ''))

caption <- sprintf('A %s: %s %s %s', escape_fun(cls), dims[[1]], times_s, dims[[2]])
caption <- sprintf('A %s: %s %s %s', escape_fun(cls), dims[[1]], chars$times_s, dims[[2]])
if (is.null(caption_override) && is_matrix) caption <- sprintf('%s of type %s', caption, escape_fun(types))

sprintf(wrap, caption, header, body)
Expand Down Expand Up @@ -237,7 +238,7 @@ repr_latex.matrix <- function(
...,
rows = getOption('repr.matrix.max.rows'),
cols = getOption('repr.matrix.max.cols'),
colspec = getOption('repr.matrix.latex.colspec')
colspec = getOption('repr.matrix.latex.colspec')
) {
cols_spec <- paste0(paste(rep(colspec$col, min(cols + 1L, ncol(obj))), collapse = ''), colspec$end)
if (has_row_names(obj)) {
Expand Down
3 changes: 2 additions & 1 deletion R/repr_vector.r
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ repr_vector_generic <- function(
# excape_fun is output format specific, encodeString ensures that non-printables come out as \-escapes
parts <- partition(length(vec), items)
charify <- function(part) escape_fun(encodeString(as.character(part), quote = if (qt) "'" else ''))
# see repr_matrix_df.r for chars$ellip_h
char_vec <-
if (is.null(parts)) charify(vec)
else c(charify(vec[parts$start]), ellip_h, charify(vec[parts$end]))
else c(charify(vec[parts$start]), chars$ellip_h, charify(vec[parts$end]))

if (!is.null(individual_wrap)) {
char_vec <- sprintf(individual_wrap, char_vec, char_vec)
Expand Down
4 changes: 4 additions & 0 deletions R/zzz_onload.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.onLoad <- function(libname = NULL, pkgname = NULL) {
onload_chars()
onload_options()
}
Loading