How to always display 3 decimal places in DataTabl

2019-02-16 20:28发布

问题:

I need to display the data in my table for 3 decimal places but it turns out that it doesn't display 3 decimal places when I run my application. Though when I try to interact with it it displays 3 decimal places.

Is there any way to do this?

回答1:

You can use DT::formatRound function. It take list of columns and number of digits to render:

library(DT)

set.seed(323)
data.frame(x=runif(10), y=rnorm(10), z=rpois(10, 1)) %>%
    datatable() %>%
    formatRound(columns=c('x', 'y'), digits=3)

Just remember about using DT::renderDataTable in the server function and DT::dataTableOutput in the UI.