insightsengineering / rtables

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

Formatting with fixed number of digits #416

Closed vpilliano closed 2 years ago

vpilliano commented 2 years ago

When I use analyze and specify a format with decimals, e.g. xx.xx, I wish the number of digits to be fixed

Example:

library(rtables)

l <- basic_table() %>%
    split_cols_by("Species") %>%
    analyze(head(names(iris), -1), afun = function(x) {
        list(
            "mean / sd" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
            "range" = rcell(diff(range(x)), format = "xx.xx")
        )
    })
l
build_table(l, iris)

This gives:

image

Even though we specified xx.xx for range, we get only one decimal (because the second is zero). This choice is questionable in the first place because the number of decimals may indicate the measures' precision. Furthermore, it becomes questionable from a look perspective when the number of decimals varies, like for virginica's Sepal.Length, which is the only value with no decimals (3).

Melkiades commented 2 years ago

On my local machine, it works as you intended. Which version are you running?

vpilliano commented 2 years ago

Thanks @Melkiades , I am using rtables 0.5.1 Could it be some R setting that differ between my environment and yours?

Melkiades commented 2 years ago

format has been slightly reworked in 5.2.x and I think this has been fixed. Try installing both formatters and rtables from GitHub and it should work

gmbecker commented 2 years ago

Indeed, this has been fixed in the development version (for quite some time now) and the fix will be present in the next CRAN release, which should be sometime this month, I believe.

> l <- basic_table() %>%
+     split_cols_by("Species") %>%
+     analyze(head(names(iris), -1), afun = function(x) {
+         list(
+             "mean / sd" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
+             "range" = rcell(diff(range(x)), format = "xx.xx")
+         )
+     })
> build_table(l, iris)
                 setosa      versicolor     virginica 
——————————————————————————————————————————————————————
Sepal.Length                                          
  mean / sd    5.01 (0.35)   5.94 (0.52)   6.59 (0.64)
  range           1.50          2.10          3.00    
Sepal.Width                                           
  mean / sd    3.43 (0.38)   2.77 (0.31)   2.97 (0.32)
  range           2.10          1.40          1.60    
Petal.Length                                          
  mean / sd    1.46 (0.17)   4.26 (0.47)   5.55 (0.55)
  range           0.90          2.10          2.40    
Petal.Width                                           
  mean / sd    0.25 (0.11)   1.33 (0.20)   2.03 (0.27)
  range           0.50          0.80          1.10    
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] formatters_0.3.3.12 tibble_3.1.6        xml2_1.3.3         
[4] testthat_3.1.3      dplyr_1.0.8         rtables_0.5.2.10   
[7] magrittr_2.0.3     

loaded via a namespace (and not attached):
 [1] zip_2.2.0         Rcpp_1.0.8.3      pillar_1.7.0      compiler_4.1.2   
 [5] base64enc_0.1-3   tools_4.1.2       uuid_1.0-4        digest_0.6.29    
 [9] pkgload_1.2.4     evaluate_0.15     lifecycle_1.0.1   pkgconfig_2.0.3  
[13] rlang_1.0.2       rstudioapi_0.13   DBI_1.1.2         cli_3.3.0        
[17] xfun_0.30         fastmap_1.1.0     stringr_1.4.0     roxygen2_7.2.1   
[21] officer_0.4.2     knitr_1.38        withr_2.5.0       systemfonts_1.0.4
[25] generics_0.1.2    desc_1.4.1        gdtools_0.2.4     vctrs_0.4.0      
[29] rprojroot_2.0.3   grid_4.1.2        tidyselect_1.1.2  glue_1.6.2       
[33] data.table_1.14.2 R6_2.5.1          fansi_1.0.3       rmarkdown_2.14   
[37] waldo_0.4.0       purrr_0.3.4       codetools_0.2-18  ellipsis_0.3.2   
[41] htmltools_0.5.2   assertthat_0.2.1  flextable_0.7.0   utf8_1.2.2       
[45] stringi_1.7.6     crayon_1.5.1      brio_1.1.3       

Closing this issue in light of the above.