Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Add a new format #880

Open
3 tasks done
mivanov5 opened this issue Apr 14, 2023 · 7 comments
Open
3 tasks done

[Feature Request]: Add a new format #880

mivanov5 opened this issue Apr 14, 2023 · 7 comments

Comments

@mivanov5
Copy link

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

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.
@clarkliming
Copy link
Contributor

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"

@Melkiades
Copy link
Contributor

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

@clarkliming
Copy link
Contributor

clarkliming commented Apr 20, 2023

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)

@Melkiades
Copy link
Contributor

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

@gmbecker
Copy link
Collaborator

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.

@shajoezhu
Copy link
Contributor

hi @mivanov5 , I think we already have the work-around in #880 (comment), would this work for you?

We can look into refactoring and improvement for future work. Thanks!

@mivanov5
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants