Skip to content

Commit

Permalink
bootstrap_class
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Oct 12, 2024
1 parent 7dffcbf commit 753561f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
35 changes: 18 additions & 17 deletions R/last_style.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
last_style <- function(sty) {
last_valid <- function(x) {
x <- x[!is.na(x)]
if (length(x) == 0) return(NA)
return(utils::tail(x, 1))
last_valid <- function(x) {
x <- x[!is.na(x)]
if (length(x) == 0) return(NA)
return(utils::tail(x, 1))
}

last_style_vec <- function(x) {
if (is.factor(x)) {
x <- as.character(x)
}
last_style_vec <- function(x) {
if (is.factor(x)) {
x <- as.character(x)
}
if (is.logical(x) && !all(is.na(x))) {
x <- any(sapply(x, isTRUE))
} else {
x <- last_valid(x)
}
return(x)
if (is.logical(x) && !all(is.na(x))) {
x <- any(sapply(x, isTRUE))
} else {
x <- last_valid(x)
}
return(x)
}


last_style <- function(sty) {
sty <- split(sty, list(sty$i, sty$j))
sty <- lapply(sty, function(k) lapply(k, last_style_vec))
sty <- do.call(rbind, lapply(sty, data.frame))
return(sty)
}


26 changes: 16 additions & 10 deletions R/style_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ setMethod(
style_apply_bootstrap <- function(x) {
sty <- x@style

if (any(!is.na(sty$bootstrap_class))) {
x@bootstrap_class <- last_style_vec(sty$bootstrap_class)
}

sty <- last_style(sty)

sty$line_color[which(sty$line_color == "black")] <- NA
Expand Down Expand Up @@ -147,16 +151,18 @@ style_apply_bootstrap <- function(x) {
css_table$id <- sapply(seq_len(nrow(css_table)), function(i) get_id(stem = "tinytable_css_"))
idx <- merge(sty[, c("i", "j", "css_arguments")], css_table)

arrays <- list()
idx <- split(idx, idx$id)
for (i in seq_along(idx)) {
id <- idx[[i]]$id[1]
arr <- sprintf("{ i: %s, j: %s }, ", idx[[i]]$i, idx[[i]]$j)
arr <- c(" {", " positions: [ ", arr, " ],", " css_id: '", id, "',", "}, ")
arr <- paste(arr, collapse = "")
x@table_string <- lines_insert(x@table_string, arr, "tinytable style arrays after", "after")
entry <- sprintf(" .table td.%s, .table th.%s { %s }", id, id, idx[[i]]$css_arguments[1])
x@table_string <- lines_insert(x@table_string, entry, "tinytable css entries after", "after")
if (nrow(idx) > 0) {
arrays <- list()
idx <- split(idx, idx$id)
for (i in seq_along(idx)) {
id <- idx[[i]]$id[1]
arr <- sprintf("{ i: %s, j: %s }, ", idx[[i]]$i, idx[[i]]$j)
arr <- c(" {", " positions: [ ", arr, " ],", " css_id: '", id, "',", "}, ")
arr <- paste(arr, collapse = "")
x@table_string <- lines_insert(x@table_string, arr, "tinytable style arrays after", "after")
entry <- sprintf(" .table td.%s, .table th.%s { %s }", id, id, idx[[i]]$css_arguments[1])
x@table_string <- lines_insert(x@table_string, entry, "tinytable css entries after", "after")
}
}

return(x)
Expand Down

0 comments on commit 753561f

Please sign in to comment.