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]: format_xx not to drop 0 after rounding #1351

Closed
3 tasks done
pzhang-cims opened this issue Nov 7, 2024 · 6 comments
Closed
3 tasks done

[Feature Request]: format_xx not to drop 0 after rounding #1351

pzhang-cims opened this issue Nov 7, 2024 · 6 comments
Assignees
Labels
enhancement question Further information is requested sme

Comments

@pzhang-cims
Copy link

Feature description

Hi there,

Many thanks for the development of {tern} package that we really enjoy using such package.

I would like to check with you about format_xx(). the format_value will have different values when utilize different codes. See below

> format_value(0.201,format='xx.xx')
[1] "0.20"
> format_value(0.201,format=format_xx('xx.xx'))
[1] "0.2"

the "0.20" is my preferred format. The reason I asked this question is i'm using analyze_vars for the calculation and formatting.

the current list_valid_format_labels() in 'rtables' do not include the format "xx.x (xx.xx)" I would like to use. So I'm trying to use format_xx in analyze_vars

library(dplyr)
library(rtables)
library(tern)
lyt<-
  
  basic_table()%>%
  
  split_cols_by(var= 'ARM')%>%
  
  analyze_vars(vars='AGE',
               
               .stats = c("mean_sd"),
               .formats = c(
                 "mean_sd" = format_xx("xx.x (xx.xx)")
               ),
               .labels = c(
                 "mean_sd" = "Mean (SD)"
               )
  )


build_table(lyt,pharmaverseadam::adsl)

The results will be

              Placebo     Xanomeline High Dose   Xanomeline Low Dose   Screen Failure
—————————————————————————————————————————————————————————————————————————————————————
Mean (SD)   75.2 (8.59)       74.4 (7.89)            75.7 (8.29)         75.1 (9.7)  

So i'm wondering if there is additional feature that using tern::analyze_vars (where I really like this function to put all the variables into the function with order), with option of .formats to specify the format? That is format_xx() to not dropping 0 at the end.

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.
@edelarua
Copy link
Contributor

edelarua commented Nov 7, 2024

Hi @pzhang-cims,

This format is not currently available as a preset in formatters or tern but you can create a custom format function as follows that should accomplish what you're looking to do:

library(dplyr)
library(rtables)
library(tern)

format_fixed_dp <- function(x, ...) {
    if (any(is.na(x))) {
        "NA"
    } else if (x[1] == 0) {
        "0"
    } else {
        sprintf("%.1f (%.2f)", x[1], x[2])
    }
}

lyt<-
    
    basic_table()%>%
    
    split_cols_by(var= 'ARM')%>%
    
    analyze_vars(vars='AGE',
                 
                 .stats = c("mean_sd"),
                 .formats = c(
                     "mean_sd" = format_fixed_dp
                 ),
                 .labels = c(
                     "mean_sd" = "Mean (SD)"
                 )
    )


build_table(lyt,pharmaverseadam::adsl)
#>               Placebo     Xanomeline High Dose   Xanomeline Low Dose   Screen Failure
#> —————————————————————————————————————————————————————————————————————————————————————
#> Mean (SD)   75.2 (8.59)       74.4 (7.89)            75.7 (8.29)        75.1 (9.70)

Created on 2024-11-07 with reprex v2.1.1

@edelarua edelarua self-assigned this Nov 7, 2024
@pzhang-cims
Copy link
Author

@edelarua thank you so much for the quick response! this really helps and i tried from my end it work!

@edelarua
Copy link
Contributor

edelarua commented Nov 8, 2024

You're welcome!

Closing this issue now.

@edelarua edelarua closed this as completed Nov 8, 2024
@shajoezhu
Copy link
Contributor

dear @pzhang-cims , could you please share this question to https://stackoverflow.com/questions/tagged/nest-tern, and @edelarua can answer there.

We would love to build the user community and better support each other in the future. Thanks a lot in advance!

@shajoezhu shajoezhu added question Further information is requested sme labels Nov 8, 2024
@pzhang-cims
Copy link
Author

pzhang-cims commented Nov 8, 2024

@shajoezhu and @edelarua, yes i'd love to support the community. I just post the questions there and feel free to wrap it up. https://stackoverflow.com/questions/79168575/format-xx-not-to-drop-0-after-rounding

@shajoezhu
Copy link
Contributor

brilliant! thanks so much guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement question Further information is requested sme
Projects
None yet
Development

No branches or pull requests

3 participants