insightsengineering / rtables

Reporting tables with R
https://insightsengineering.github.io/rtables/
Other
219 stars 48 forks source link

slightly relax label validity check #871

Open gmbecker opened 2 months ago

gmbecker commented 2 months ago

Currently any use of "{" or "}" in a label is disallowed here:

https://github.com/insightsengineering/rtables/blob/bdafa9d155ef71c7bc829d17c5ba78ae5a7c8308/R/00tabletrees.R#L23

This is due to formatters historically/currently being somewhat overly aggresive in guessing where referential footnotes are

I'd like to relax this to checking for the pattern " [{][^}]+[}]" (including the space)

This is due to formatting instructions being embedded in strings in the form, e.g., "~{super a}" for superscripted a in study metadata tracking tools. I didn't choose this, but I also can't change it, and with the leading ~ it is distinguishable from a referential footnote.

This will require a change to how refdefs are inferred in formatters to be safe, in particular, here:

https://github.com/insightsengineering/formatters/blob/63530282db089234559b865ef031814907ec6075/R/matrix_form.R#L381

Splitting that one gsub into two regexes, one grep to detect them which has the space and then the gsub to extract them, should be sufficient, e.g.,

refs <- matrix("", nrow = nrow(strs), ncol = ncol(strs))
haveref <- grepl(" [{][^}]*[}]", strs) ## with space
refs[haveref] <-  gsub("^[^{]*([{]([^}]+)[}]){0,1}$", "\\2", strs[haveref]) ## without space, but only ones we know had one
Melkiades commented 2 months ago

Good point! Checked, and the expected error message if there are {} in labels is the following:

Error in check_ok_label(label) : 
  Labels cannot contain { or } due to their use for indicating referential footnotes