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

Scientific notation format function for DT #938

Open
3 tasks done
lrosaleny opened this issue Oct 25, 2021 · 7 comments · May be fixed by #949
Open
3 tasks done

Scientific notation format function for DT #938

lrosaleny opened this issue Oct 25, 2021 · 7 comments · May be fixed by #949

Comments

@lrosaleny
Copy link

Hi,
I'm trying to format some columns with scientific notation and finding it very difficult. I have been trying to write a short javascript snippet to take advantage of toExponential using rowCallback, but to no avail (see example in https://stackoverflow.com/questions/49634329/how-to-force-scientific-notation-with-renderdt).

I was wondering if a formatScientific function could be created, as the ones that DT already has (formatCurrency, formatRound, etc ...)

This formatScientific could use as argument the number of decimal digits to use for the significand (or mantissa) in order to get a number with normalized scientific notation (https://en.wikipedia.org/wiki/Scientific_notation)

Thank you very much to all the contributors in this repo. Your work is highly appreciated!


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.name/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('DT'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/DT').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@shrektan
Copy link
Collaborator

The simplest way of doing this is to convert the numeric column into a character column format(..., scientific=TRUE) before passing to DT.

@lrosaleny
Copy link
Author

lrosaleny commented Dec 1, 2021

Hi, this could work, but I would like to be able to order (in descending/ascending order) the values in the columns where the scientific notation is used. If the values are characters I'm not sure the ordering would work. Thank you, anyway.

@shrektan shrektan linked a pull request Dec 5, 2021 that will close this issue
9 tasks
@shrektan
Copy link
Collaborator

shrektan commented Dec 5, 2021

@lrosaleny After PR #949 being merged, you can do it by using the new formatter args and the ordering would be preserved.

tbl = cbind(cars, cars)
colnames(tbl) = c("speed", "dist", "speed_raw", "dist_raw")
DT::datatable(tbl, formatter = list(
  speed = function(x) format(x, scientific = TRUE),
  dist = function(x) format(x, scientific = TRUE)
))

image

@egoipse
Copy link

egoipse commented Jul 11, 2022

Hi,

I get an unused argument error when adding formatter argument.

Error in datatable(rename(cbind(as.data.frame(el_chi[1:3]), as.data.frame(assocstats(tabla)[4:5])),  : 
  unused argument (formatter = list(p.value = function(x) format(x, scientific = T, digits = 2)))

Was formatter argument officially included in the package? I don't see any reference to it in help documentation.

Session info:

R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C               LC_TIME=es_CL.UTF-8       
 [4] LC_COLLATE=es_ES.UTF-8     LC_MONETARY=es_CL.UTF-8    LC_MESSAGES=es_ES.UTF-8   
 [7] LC_PAPER=es_CL.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=es_CL.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] vcd_1.4-10                ai.surveykitr_0.0.0.9000  RVAideMemoire_0.9-81-2   
 [4] xaringanExtra_0.6.0       dai.estudios.r_0.0.0.9000 forcats_0.5.1            
 [7] stringr_1.4.0             dplyr_1.0.9               purrr_0.3.4              
[10] readr_2.1.2               tidyr_1.2.0               tibble_3.1.7             
[13] ggplot2_3.3.6             tidyverse_1.3.1           rcompanion_2.4.16        
[16] broom_1.0.0               DT_0.23                   here_1.0.1               
[19] pacman_0.5.1             

loaded via a namespace (and not attached):
  [1] TH.data_1.1-1       colorspace_2.0-3    ellipsis_0.3.2      class_7.3-20        modeltools_0.2-23  
  [6] rsconnect_0.8.26    rprojroot_2.0.3     fs_1.5.2            gld_2.6.5           rstudioapi_0.13    
 [11] proxy_0.4-27        farver_2.1.1        xaringan_0.25       remotes_2.4.2       fansi_1.0.3        
 [16] mvtnorm_1.1-3       lubridate_1.8.0     coin_1.4-2          xml2_1.3.3          codetools_0.2-18   
 [21] splines_4.2.1       cachem_1.0.6        rootSolve_1.8.2.3   libcoin_1.0-9       knitr_1.39         
 [26] pkgload_1.3.0       jsonlite_1.8.0      dbplyr_2.2.1        BiocManager_1.30.18 compiler_4.2.1     
 [31] httr_1.4.3          backports_1.4.1     assertthat_0.2.1    Matrix_1.4-1        fastmap_1.1.0      
 [36] cli_3.3.0           htmltools_0.5.2     prettyunits_1.1.1   tools_4.2.1         gtable_0.3.0       
 [41] glue_1.6.2          lmom_2.9            Rcpp_1.0.8.3        jquerylib_0.1.4     cellranger_1.1.0   
 [46] vctrs_0.4.1         crosstalk_1.2.0     lmtest_0.9-40       xfun_0.31           ps_1.7.1           
 [51] openxlsx_4.2.5      rvest_1.0.2         lifecycle_1.0.1     devtools_2.4.3      MASS_7.3-57        
 [56] zoo_1.8-10          scales_1.2.0        hms_1.1.1           parallel_4.2.1      sandwich_3.0-2     
 [61] expm_0.999-6        yaml_2.3.5          Exact_3.1           memoise_2.0.1       sass_0.4.1         
 [66] stringi_1.7.6       nortest_1.0-4       e1071_1.7-11        zip_2.2.0           boot_1.3-28        
 [71] pkgbuild_1.3.1      rlang_1.0.3         pkgconfig_2.0.3     matrixStats_0.62.0  evaluate_0.15      
 [76] lattice_0.20-45     labeling_0.4.2      htmlwidgets_1.5.4   processx_3.7.0      tidyselect_1.1.2   
 [81] plyr_1.8.7          magrittr_2.0.3      R6_2.5.1            DescTools_0.99.45   generics_0.1.3     
 [86] multcompView_0.1-8  multcomp_1.4-19     DBI_1.1.3           pillar_1.7.0        haven_2.5.0        
 [91] withr_2.5.0         survival_3.2-13     modelr_0.1.8        crayon_1.5.1        utf8_1.2.2         
 [96] tzdb_0.3.0          rmarkdown_2.14      usethis_2.1.6       readxl_1.4.0        data.table_1.14.2  
[101] callr_3.7.0         reprex_2.0.1        digest_0.6.29       stats4_4.2.1        munsell_0.5.0      
[106] bslib_0.3.1         sessioninfo_1.2.2 

@stla
Copy link
Collaborator

stla commented Jul 13, 2022

@egoipse
Copy link

egoipse commented Jul 13, 2022

Thanks... I saw that post and all answers before to reach this issue.

None of the answers explains what happened with formatter.

@shrektan
Copy link
Collaborator

@egoipse the formatter argument has not been merged into DT, yet. I can't find any time in the coming months to finish this. Please use alternative solutions. Thanks!

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

Successfully merging a pull request may close this issue.

4 participants