insightsengineering / formatters

A framework for creating listings of raw data that include specialized formatting, headers, footers, referential footnotes, and pagination.
https://insightsengineering.github.io/formatters/
Other
15 stars 6 forks source link

Bug in table export #288

Closed BFalquet closed 2 months ago

BFalquet commented 2 months ago

Reproducible example:

library(rtables)
lyt <- basic_table() %>%
  split_cols_by("Species") %>%
  analyze("Sepal.Length", afun = function(x) {
    list(
      "mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
      "range" = diff(range(x))
    )
  })
lyt

tbl <- build_table(lyt, iris)
prov_footer(tbl) <- c("Santa", "is", "real", "!")
export_as_txt(tbl)

leads to

Error in length(prov_footer(obj)) == 0 || !grepl(page_num, prov_footer(obj),  : 
  'length = 4' in coercion to 'logical(1)'

I think there is just a any() missing to handle the case where the prov footer is a vector of several elements.

Melkiades commented 2 months ago

@BFalquet I cannot reproduce the error

BFalquet commented 2 months ago

@Melkiades There is also something weird happening with the dependencies today, but just from a purely formal code analysis point of view, the following code in paginate_to_mpfs has a small issue:

(length(prov_footer(obj)) == 0 || !grepl(page_num, prov_footer(obj), perl = TRUE))

If the length of prov_footer(obj) is bigger than 1, the grepl expression will return a logical vector bigger than 1 that will cause an issue because here the || expects two vectors of size 1. E.g

> FALSE || grepl("a", letters)
Error in FALSE || grepl("a", letters) : 
  'length = 26' in coercion to 'logical(1)'

It doesn't fail every time, because if the first part of the expression is TRUE, the second part is not evaluated but in the other cases, we have an issue.

Anyway, this makes the non-CRAN tests of one of my internal package fail.

Melkiades commented 2 months ago

ok I see! let me generalize the fix today

edelarua commented 2 months ago

Should be fixed by #290 - bug was still occurring when prov_footer is not empty but it's no longer an issue in my tests.