Skip to content

Commit

Permalink
bugfix d column with empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Dec 27, 2024
1 parent dcf4289 commit 503baf6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: tinytable
Type: Package
Title: Simple and Configurable Tables in 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', and 'Typst' Formats
Description: Create highly customized tables with this simple and dependency-free package. Data frames can be converted to 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', or 'Typst' tables. The user interface is minimalist and easy to learn. The syntax is concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package.
Version: 0.6.1.5
Version: 0.6.1.6
Imports:
methods
Depends:
Expand All @@ -17,6 +17,7 @@ Suggests:
gh,
marginaleffects,
markdown,
modelsummary,
pandoc,
quarto,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Bugs:

* `save_tt("file.pdf")` works with colors. Thanks to @olivedv for the report and solution #395.
* `group_tt(i=vec)`: `vec` can be a factor vector
* `style_tt(align="d")` with empty strings (`modelsummary::datasummary_balance()` test)

New:

Expand Down
6 changes: 6 additions & 0 deletions R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,14 @@ color_to_preamble <- function(x, col) {
get_dcolumn <- function(j, x) {
siunitx <- get_option("tinytable_siunitx_table_format", default = "table-format=-%s.%s,table-align-text-before=false,table-align-text-after=false,input-symbols={-,\\*+()}")
num <- unlist(x@table_dataframe[, j])

# empty cells
num <- sapply(num, trimws)
num <- num[sapply(num, nchar) > 0]

num <- strsplit(num, "\\.")
num <- lapply(num, function(k) if (length(k) == 1) c(k, " ") else k)

left <- sapply(num, function(k) k[[1]])
right <- sapply(num, function(k) k[[2]])
left <- max(nchar(gsub("\\D", "", left)))
Expand Down
20 changes: 20 additions & 0 deletions inst/tinytest/test-modelsummary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
source("helpers.R")
requiet("modelsummary")

# modelsummary Issue #711: stars and d-columns
training <- 'https://vincentarelbundock.github.io/Rdatasets/csv/Ecdat/Treatment.csv'
training <- read.csv(training, na.strings = "")
training <- transform(training,
`Earnings Before` = re75 / 1000,
`Earnings After` = re78 / 1000,
Treatment = ifelse(treat == TRUE, 'Treatment', 'Control'),
Married = ifelse(married == TRUE, 'Yes', 'No')
)
cols <- c("Earnings Before", "Earnings After", "Treatment", "ethn", "age", "educ", "Married")
names <- c("Earnings Before", "Earnings After", "Treatment", "Ethnicity", "Age", "Education", "Married")
training <- training[, cols]
training <- setNames(training, names)
tab <- datasummary_balance(~ Treatment, training, fmt = 3, stars = TRUE, output = "data.frame")
p <- tt(tab) |> style_tt(align = "lllllldl") |> save_tt("latex")
expect_inherits(p, "character")

0 comments on commit 503baf6

Please sign in to comment.