如何在R Shiny中的数据表中更改为24小时格式 [英] How to change to 24 hour format in datatables in R Shiny
问题描述
就像大多数人一样,时间日期格式也不是R中我最喜欢的主题,并且再次给我带来了比我讨价还价更大的麻烦。
此问题的后续操作:
库(发光)
库(DT)
数据<-结构(列表(DATUM =结构(c(1490738402、1490738436、1490738440,
1490738444、1490738447、1497538451、1490738455、1490738459、1490738463,
1490738467)) = c( POSIXct, POSIXt),tzone = CEST),NUMMER = c(19,
20,21,22,23,24,25,26,27,28)),。名称= c( DATUM, NUMMER),row.names = c(NA,10L),类= data.frame)
tz<-Sys.timezone()
data $ DATUM<-as.POSIXct(as.character(data $ DATUM),tz = tz)
ui = fluidPage(
dataTableOutput( tab)
)
server =函数(输入,输出,会话){
output $ tab<-DT :: renderDataTable({
datatable(data,rownames = TRUE,filter = top,class ='cell-border stripe')%>%
formatDate(1,method ='toLocaleString')})
}
ShinyApp(ui,server)
您可以尝试将 DT :: formatDate
中的方法
参数更改为不同的日期时间格式,请检查?DT :: formatDate
或 DT帮助器功能。如果这些方法均不能提供正确的输出,则可以使用 format
手动设置日期时间输出的格式,例如
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
如果您不想要更改原始data.frame中的POSIXct值,您只能在render-function中更新日期时间格式。以下是使用 dplyr
的一种方法:
library(shiny )
库(DT)
库(dplyr)
data<-structure(list(DATUM = structure(c(1490738402,1490738436,1490738440,
1490738444 ,1490738447,1490738451,1490738455,1490738459,1490738463,
1490738467),类= 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)
)
服务器=函数(输入,输出,会话){
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)
请注意,在您的特定示例中,如果未必要求日期为POSIXct类,那么将日期直接解析为字符串可能就足够了:
data<-structure(list(DATUM = structure(c(1490738402,1490738436,1490738440,
1490738444、1490738447、1490738451、1490738455、1490738459、1490738463,
1490738467),类= c( POSIXct, POSIXt),tzone = CEST),NUMMER = c(19,
20、21、22、23、24、25、26、27、28))。.名称= c( DATUM, NUMMER),row.names = c(NA,10L),类=数据。 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
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)
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"
这篇关于如何在R Shiny中的数据表中更改为24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!