tern
tern copied to clipboard
[Feature Request]: Add a new format
Feature description
In the list of possible formats for ratables/tern functions (produced by list_valid_format_labels()
, formatters package) the following format is missing:
(xx. - xx.)
We have similar formats like (xx., xx.)
and xx. (xx.%)
, but not the one above.
Please add this format.
Code of Conduct
- [X] I agree to follow this project's Code of Conduct.
Contribution Guidelines
- [X] I agree to follow this project's Contribution Guidelines.
Security Policy
- [X] I agree to follow this project's Security Policy.
I do have a idea that we somehow redesign the "xx.x" things, to make it flexible (and translate it into "sprintf"). e.g.,
"sadbasd asa a xx.xx asda a" translate to "sadbasd asa a %.2f asda a"
This possibility exists and it is working very fine:
library(rtables)
fmtfun <- sprintf_format("this range => %.1f")
format_value(100, format = fmtfun)
fmtfun2 <- sprintf_format("sadbasd asa a %.2f asda a %.1f")
format_value(list(12.23456, 2.724), format = fmtfun2)
lyt <- basic_table() %>%
split_cols_by("Species") %>%
analyze("Sepal.Length", afun = function(x) {
list(
"mean (sd)" = rcell(c(mean(x), sd(x)), format = fmtfun2),
"range" = rcell(diff(range(x)), format = fmtfun)
)
})
> build_table(lyt, iris)
setosa versicolor virginica
—————————————————————————————————————————————————————————————————————————————————————————————————————————
mean (sd) sadbasd asa a 5.01 asda a 0.4 sadbasd asa a 5.94 asda a 0.5 sadbasd asa a 6.59 asda a 0.6
range this range => 1.5 this range => 2.1 this range => 3.0
I think this MUST be better documented though as it is a very powerful and elegant solution to multiple little problems
oh I mean we have two sets of printing format, one is from "printf_format", one is translating "xx.xx" into "%.2f" so that users could use any "xx.xx" to construct there format (as this is more common in practice for tlg works)
I see what you mean. I think it is a bit of overhead for little gain as you would use directly sprintf_format
if you need to do that. I think the way format currently handles "xx" is a bit more complete than its printf
cousin as it handles strings and NAs for any rcell specifically. I think doing what you suggested will end up losing functionality. I could be wrong though, maybe @gmbecker thinks differently
This essentially falls under factoring formatters format detection into a parser, which is on the roadmap, but a large piece of work we have not yet had the resources to pursue.
hi @mivanov5 , I think we already have the work-around in https://github.com/insightsengineering/tern/issues/880#issuecomment-1516207094, would this work for you?
We can look into refactoring and improvement for future work. Thanks!
Hi @shajoezhu, I made another modification to my program, but guess that as suggested above next time I can create my own format and use its name instead of "xx. - xx.". Hope that anyway it will be added to the formats list at some point. Thanks to @Melkiades for the example and to everyone else for your comments.