diff --git a/R/style_typst.R b/R/style_typst.R index 43a73fe8..91f5cab9 100644 --- a/R/style_typst.R +++ b/R/style_typst.R @@ -86,22 +86,37 @@ setMethod( if (length(fontsize) == 1) fontsize <- rep(fontsize, nrow(settings)) indent_value <- if (indent > 0) paste0(indent, "em") else "false" counter <- 0 - for (idx in seq_len(nrow(settings))) { - k <- settings[idx, "i"] - w <- settings[idx, "j"] + settings$color <- color + settings$underline <- underline + settings$italic <- italic + settings$bold <- bold + settings$monospace <- monospace + settings$strikeout <- strikeout + settings$fontsize <- fontsize + settings$indent <- indent_value + sp <- split(settings, settings[, 3:ncol(settings)]) + sp <- lapply(sp, function(x) { + x$i <- sprintf("(%s,)", paste(unique(x$i), collapse = ", ")) + x$j <- sprintf("(%s,)", paste(unique(x$j), collapse = ", ")) + x[1, , ] + }) + sp <- do.call(rbind, sp) + for (idx in seq_len(nrow(sp))) { + k <- sp[idx, "i"] + w <- sp[idx, "j"] counter <- counter + 1 style <- sprintf( " (y: %s, x: %s, color: %s, underline: %s, italic: %s, bold: %s, mono: %s, strikeout: %s, fontsize: %s, indent: %s),", k, w, - color[counter], - tolower(underline[counter]), - tolower(italic[counter]), - tolower(bold[counter]), - tolower(monospace[counter]), - tolower(strikeout[counter]), - fontsize[counter], - indent_value + sp$color[counter], + tolower(sp$underline[counter]), + tolower(sp$italic[counter]), + tolower(sp$bold[counter]), + tolower(sp$monospace[counter]), + tolower(sp$strikeout[counter]), + sp$fontsize[counter], + sp$indent[counter] ) out <- lines_insert(out, style, "tinytable cell style after", "after") } @@ -110,15 +125,23 @@ setMethod( if (fill_style_flag) { if (length(background) == 1) background <- rep(background, nrow(settings)) counter <- 0 - for (idx in seq_len(nrow(settings))) { - k <- settings[idx, "i"] - w <- settings[idx, "j"] + settings$background <- background + sp <- split(settings, settings$background) + sp <- lapply(sp, function(x) { + x$i <- sprintf("(%s,)", paste(unique(x$i), collapse = ", ")) + x$j <- sprintf("(%s,)", paste(unique(x$j), collapse = ", ")) + x[1, , ] + }) + sp <- do.call(rbind, sp) + for (idx in seq_len(nrow(sp))) { + k <- sp[idx, "i"] + w <- sp[idx, "j"] counter <- counter + 1 fill <- sprintf( " (y: %s, x: %s, fill: %s),", k, w, - background[counter]) + sp$background[counter]) out <- lines_insert(out, fill, "tinytable cell fill after", "after") } } @@ -140,15 +163,23 @@ setMethod( } else if (length(align_value) %in% c(1, nrow(settings))) { if (length(align_value) == 1) align_value <- rep(align_value, nrow(settings)) counter <- 0 - for (idx in seq_len(nrow(settings))) { - k <- settings[idx, "i"] - w <- settings[idx, "j"] + settings$align <- align_value + sp <- split(settings, settings$align) + sp <- lapply(sp, function(x) { + x$i <- sprintf("(%s,)", paste(unique(x$i), collapse = ", ")) + x$j <- sprintf("(%s,)", paste(unique(x$j), collapse = ", ")) + x[1, , ] + }) + sp <- do.call(rbind, sp) + for (idx in seq_len(nrow(sp))) { + k <- sp[idx, "i"] + w <- sp[idx, "j"] counter <- counter + 1 fill <- sprintf( " (y: %s, x: %s, align: %s),", k, w, - align_value[counter]) + sp$align[counter]) out <- lines_insert(out, fill, "tinytable cell align after", "after") } } else { diff --git a/inst/templates/typst.typ b/inst/templates/typst.typ index 6fa8fef0..b3f5d575 100644 --- a/inst/templates/typst.typ +++ b/inst/templates/typst.typ @@ -23,7 +23,7 @@ block[ // start block // tinytable align-default-array after #show table.cell: it => { let tmp = it - let data = style-array.find(data => data.x == it.x and data.y == it.y) + let data = style-array.find(data => data.x.contains(it.x) and data.y.contains(it.y)) if data != none { set text(data.color) set text(data.fontsize) @@ -44,7 +44,7 @@ block[ // start block #table( // tinytable table start stroke: none, align: (x, y) => { - let data = align-array.find(data => data.x == x and data.y == y) + let data = align-array.find(data => data.x.contains(x) and data.y.contains(y)) if data != none { data.align } else { @@ -52,7 +52,7 @@ block[ // start block } }, fill: (x, y) => { - let data = fill-array.find(data => data.x == x and data.y == y) + let data = fill-array.find(data => data.x.contains(x) and data.y.contains(y)) if data != none { data.fill } diff --git a/sandbox/typst.qmd b/sandbox/typst.qmd new file mode 100644 index 00000000..f8f4c9e9 --- /dev/null +++ b/sandbox/typst.qmd @@ -0,0 +1,128 @@ +--- +title: "`tinytable` Typst examples" +format: typst +keep-typ: true +--- + +```{r} +pkgload::load_all() +``` + +```{r} +# Semi-complicated +tab <- tt(mtcars[1:4, 1:5], caption = "Hell}}o World") |> + group_tt(j = list("Group 1" = 4:5, "Group 2" = 2:3)) |> + style_tt(j = 1:5, align = "lcccr") |> + style_tt(i = 2, j = 1:3, strikeout = TRUE, bold = TRUE, background = "black", color = "white") |> + style_tt(j = 1, color = "red", italic = TRUE) +tab +``` + +```{r} +# Issue #139 +tab <- tt(mtcars[1:10, 1:5]) |> + group_tt(i = list("Feta" = 2, "Brie" = 6)) |> + group_tt(j = list("First" = 2:3, "Second" = 4:5)) |> + style_tt(1:5, align = "c", background = "blue", color = "white") +tab +``` + +```{r} +# Theme striped +x <- mtcars[1:4, 1:5] +tab <- tt(x, theme = "striped") +tab +``` + + +```{r} +# Formatting +dat <- data.frame( + w = c(143002.2092, 201399.181, 100188.3883), + x = c(1.43402, 201.399, 0.134588), + y = as.Date(c(897, 232, 198), origin = "1970-01-01"), + z = c(TRUE, TRUE, FALSE)) +dat <- tt(dat, digits = 2) +dat +``` + +```{r} +# More formatting +dat <- data.frame( + a = c("Burger", "Halloumi", "Tofu", "Beans"), + b = c(1.43202, 201.399, 0.146188, 0.0031), + c = c(98938272783457, 7288839482, 29111727, 93945)) +tab <- tt(dat) |> + format_tt(j = "a", sprintf = "Food: %s") |> + format_tt(j = 2, digits = 1) |> + format_tt(j = "c", digits = 2, num_suffix = TRUE) +tab +``` + +```{r} +# Significant cell +dat <- data.frame(x = c(0.000123456789, 12.4356789)) +tab <- tt(dat) |> format_tt(digits = 2, num_fmt = "significant_cell") +tab +``` + +```{r} +# Missing value replacement +tab <- tt(data.frame(a = c(NA, 1, 2), b = c(3, NA, 5))) +tab <- format_tt(tab, replace = "-") +tab +``` + +```{r} +# Italic markdown +dat <- data.frame(markdown = c("This is _italic_ text.")) +tab <- tt(dat) |> + format_tt(j = 1, markdown = TRUE) |> + style_tt(j = 1, align = "c") +tab +``` + +```{r} +# Font size +dat <- tt(x) |> style_tt(j = "mpg|hp|qsec", fontsize = 1.5) +dat +``` + +```{r} +# No headers +k <- x +colnames(k) <- NULL +k <- tt(k) +k +``` + +```{r} +# Group rows +dat <- mtcars[1:9, 1:8] +dat <- tt(dat) |> + group_tt(i = list( + "I like (fake) hamburgers" = 3, + "She prefers halloumi" = 4, + "They love tofu" = 7)) +dat +``` + +```{r} +# Group columns +dat <- mtcars[1:9, 1:8] +tab <- tt(dat) |> + group_tt( + j = list( + "Hamburgers" = 1:3, + "Halloumi" = 4:5, + "Tofu" = 7)) +tab +``` + +````{comment} +```{r} +# Theme grid +tab <- tt(x, theme = "grid") +tab +``` +```` \ No newline at end of file