I would like the behaviour of numeric values in DT
tables to be the same than in the print
output when using :
options(scipen = -1)
options(digits = 3)
cars/1000000
But whatever are the options, it seems that DT do not care about it:
I know that print
is not the same as rendering a table, but there should be a way to do it. I can play with signif
or round
to limit digits but I am losing information with very low values and this affects high values differently.
Here is the minimal example.
library(shiny)
library(DT)
library(dplyr)
options(scipen = -1)
options(digits = 3)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
DTOutput("dt"),
DTOutput("dt2")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$dt <- renderDT({cars/1000000})
output$dt2 <- renderDT({
mutate_all(cars/1000000, funs(signif(., digits = 1)))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Any clues ?
The DT
package also has a formatSignif() function that can help with this, e.g.:
output$tbl <- renderDataTable({
DT::datatable(dat) %>%
formatSignif(columns = c('speed', 'dist'), digits = 3)
})
You could try using a rowCallback
to change the notation to scientific using the toExponential
javascript function.
Here's an example:
library(shiny)
library(DT)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
DTOutput("dt")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$dt <- renderDT({
datatable(cars/10,options = list(
rowCallback = JS(
"function(row, data) {",
"for (i = 1; i < data.length; i++) {",
"if (data[i]>1000 | data[i]<1){",
"$('td:eq('+i+')', row).html(data[i].toExponential(1));",
"}",
"}",
"}")
)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With