问题
Just like most people, time date format is also not my favorite topic in R, and is once again giving me more trouble than I bargained for.
In follow-up of this question: SO
I got rid of the 'T' and 'Z' with 'toLocaleString'
but now my datatable
is showing times in AM and PM, while I just want to see the original 24h times.
I'm running the app in google chrome, and my output looks like this at the moment:
library(shiny)
library(DT)
data <- structure(list(DATUM = structure(c(1490738402, 1490738436, 1490738440,
1490738444, 1490738447, 1490738451, 1490738455, 1490738459, 1490738463,
1490738467), class = c("POSIXct", "POSIXt"), tzone = "CEST"), NUMMER = c(19,
20, 21, 22, 23, 24, 25, 26, 27, 28)), .Names = c("DATUM", "NUMMER"), row.names = c(NA, 10L), class = "data.frame")
tz <- Sys.timezone()
data$DATUM <- as.POSIXct(as.character(data$DATUM), tz=tz)
ui=fluidPage(
dataTableOutput("tab")
)
server= function(input, output,session) {
output$tab <- DT::renderDataTable({
datatable(data,rownames=TRUE, filter="top", class = 'cell-border stripe') %>%
formatDate(1, method = 'toLocaleString')})
}
shinyApp(ui, server)
回答1:
You could try changing the method
argument in DT::formatDate
to a different date-time format, check ?DT::formatDate
or DT Helper Functions. If none of these methods gives the right output, you can manually format the date-time output with format
, e.g.
data$DATUM <- format(data$DATUM, "%d/%m/%Y, %H:%M:%S")
#> [1] "28/03/2017, 22:00:02" "28/03/2017, 22:00:36" "28/03/2017, 22:00:40"
#> [4] "28/03/2017, 22:00:44" "28/03/2017, 22:00:47" "28/03/2017, 22:00:51"
#> [7] "28/03/2017, 22:00:55" "28/03/2017, 22:00:59" "28/03/2017, 22:01:03"
#> [10] "28/03/2017, 22:01:07"
If you do not want to change the POSIXct-values in the original data.frame, you could update the date-time format only inside the render-function. Below just one way of doing it with dplyr
:
library(shiny)
library(DT)
library(dplyr)
data <- structure(list(DATUM = structure(c(1490738402, 1490738436, 1490738440,
1490738444, 1490738447, 1490738451, 1490738455, 1490738459, 1490738463,
1490738467), class = c("POSIXct", "POSIXt"), tzone = "CEST"), NUMMER = c(19,
20, 21, 22, 23, 24, 25, 26, 27, 28)), .Names = c("DATUM", "NUMMER"), row.names = c(NA, 10L), class = "data.frame")
tz <- Sys.timezone()
data$DATUM <- as.POSIXct(as.character(data$DATUM), tz=tz)
ui=fluidPage(
dataTableOutput("tab")
)
server= function(input, output,session) {
output$tab <- DT::renderDataTable({
mutate(data, DATUM = format(DATUM, "%d/%m/%Y, %H:%M:%S")) %>%
datatable(rownames=TRUE, filter="top", class = 'cell-border stripe')
})
}
shinyApp(ui, server)
Note that in your specific example, if the dates are not necessarily required to be POSIXct class, it might be sufficient to just parse the dates as strings directly:
data <- structure(list(DATUM = structure(c(1490738402, 1490738436, 1490738440,
1490738444, 1490738447, 1490738451, 1490738455, 1490738459, 1490738463,
1490738467), class = c("POSIXct", "POSIXt"), tzone = "CEST"), NUMMER = c(19,
20, 21, 22, 23, 24, 25, 26, 27, 28)), .Names = c("DATUM", "NUMMER"), row.names = c(NA, 10L), class = "data.frame")
(data$DATUM <- as.character(data$DATUM))
#> [1] "2017-03-28 22:00:02" "2017-03-28 22:00:36" "2017-03-28 22:00:40"
#> [4] "2017-03-28 22:00:44" "2017-03-28 22:00:47" "2017-03-28 22:00:51"
#> [7] "2017-03-28 22:00:55" "2017-03-28 22:00:59" "2017-03-28 22:01:03"
#> [10] "2017-03-28 22:01:07"
来源:https://stackoverflow.com/questions/56818869/how-to-change-to-24-hour-format-in-datatables-in-r-shiny