rtables
rtables copied to clipboard
It should be easier to align long footnotes to table
I'm not sure if this is an issue for rtables or better suited for another package but if there are several lines of footnotes, there's no easy for users to get a nice alignment.
For example, I get the layout below footnotes with export_as_pdf
and by manually inserting line breaks. It would be much easier to have some utility where I can provide one long string without line breaks where the optimal width can be determined automatically to alight with the table width.
Hi @anajens
The current contract is that the footers can be multiple elements, but that the elements will not be additionally wrapped/split. So you don't need line breaks per se, you should be able to just give a vector of the lines you want.
> analysisfun <- function(x, ...) {
+ in_rows(row1 = 5,
+ row2 = c(1, 2),
+ .row_footnotes = list(row1 = "row 1 rfn"),
+ .cell_footnotes = list(row2 = "row 2 cfn"))
+ }
> lyt <- basic_table(title = "Title says Whaaaat", subtitles = "Oh, ok.",
+ main_footer = c("ha HA! Footer!", "oh snap! again!")) %>%
+ split_cols_by("ARM") %>%
+ analyze("AGE", afun = analysisfun)
> result <- build_table(lyt, ex_adsl)
> result
Title says Whaaaat
Oh, ok.
--------------------------------------------------
A: Drug X B: Placebo C: Combination
--------------------------------------------------
row1 {1} 5 5 5
row2 1, 2 {2} 1, 2 {3} 1, 2 {4}
--------------------------------------------------
{1} - row 1 rfn
{2} - row 2 cfn
{3} - row 2 cfn
{4} - row 2 cfn
--------------------------------------------------
ha HA! Footer!
oh snap! again!
It is important for pagination that the by the time that process is occuring all splitting has already occurred. (Because pagination needs to know how many lines something takes up).
We don't currently have any plans to add word-wrap/string-splitting utilities into rtables. I suppose we could but I dont' really see why, its a more general (and I have to suspect, probably already solved?) problem.
Thanks Gabe, I was thinking more about the main_footer
string. That one can't be split into multiple rows aside from inserting line breaks, right?
@anajens if you look at the code above you'll see that main_footer is being given as a vector with multiple elements each of which is given its own line. I'm actually not sure what would happen if one or more of the elements is too long, but if that isn't the case, you can pre-separate it. No need to inject actual new-line characters into a single string.
I should have been more clear about calling attention to that.
Sorry yes, I see that now.
Is there some internal utility function that export_as_pdf
uses to determine the overall width of the table across pages? If that was made available to users, then it would make it much easier to know into how many substrings to separate the main footer.
export_as_pdf
accepts the width as an argument, along with the height and margins.
Internally it uses grid functionality to check widths. @waddella is the resident expert, but I think something along the lines of the following will tell you if something is too wid eor not (I don't deal with margins there so its not quite right but pretty close.
check_txt_width <- function(txts, fontsize = 8, fontfamily = "mono", max_width = 11.7) {
tmpf <- tempfile()
pdf(tmpf)
gp <- gpar(fontsize = fontsize, fontfamily = fontfamily)
res <- vapply(
txts, function(txt) {
txg <- textGrob(txt, x = 0, y = 1, gp = gp)
convertWidth(grobWidth(txg), "inches", valueOnly = TRUE)
}, 1.0, USE.NAMES = FALSE)
dev.off()
res <= max_width
}
> check_txt_width(c("hi", "still ok but muchlonger", "omgthisismuchtoolongomgogomgomgomgogmogmgomgomgomgogmgomgomgomgogmgomgomgogmgomgomgomgomgomgomgomgomgomgomgomgogmgomgomgomgomgomgomgomgolmgomgomgomgogmgomgomgogmgomgomgomgomgomgomgomgomgomgomgomgogmgomgomgomgomgomgomgomgolmgomgomgomgogmgomgomgomgomg"))
[1] TRUE TRUE FALSE
Figuring out where to word wrap it if it is too long so that it looks good is a much complicated problem if you want to do it right. stringi::str_wrap
is likely to be better than anything we will ever provide, though even it doesn't do the really nice hyphen-when-you-need-it style wrapping from what I can see.
@anajens is this still something that needs attention or what is in ther enow sufficient?
I agree that using something like stringi::str_wrap
is probably sufficient. But users would still need to to know how many characters are allowed to fit on a line based on the table width. I think some kind of utility function for that would be helpful to have in rtables.
Also when thinking about the workflow, it would need to be something like this:
- generate table
- figure out width of table
- annotate table with main footer wrapped in
stringi::str_wrap(width = tbl_width)
This is all for cosmetic purposes and not critical at all but imagine it's something other users will bring up too.
This was done, all aspects of a table's rendering are now wrapped to max_width
(or smaller, in the case of individual column labels and cells)