Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R using formatRound for all numeric columns of a datatable

The following R code can be used to format the first four columns of iris data. Wondering how to format all numeric columns rather than giving their names or positions.

library(tidyverse)
library(DT)

iris %>% 
  datatable() %>% 
  formatRound(1:4, digits = 2)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Perhaps, we can do this before converting to datatable

library(dplyr)
library(DT)
iris %>%
  mutate(across(where(is.numeric), sprintf, fmt = '%.2f')) %>%
  datatable() 

Or another option is to check the ‘data’ component in the created object

iris %>% 
     datatable()  %>%
     formatRound(purrr::map_lgl(.$x$data, is.numeric), digits = 2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading