如何解决 timevis R 上的时区问题?
How can I resolve the timezones issue on timevis R?
我需要取timevis返回的数据,但是返回的这些数据比输入的数据少了一小时。我也尝试过更改 R 时区和服务器,但这并没有解决问题。
这是我正在尝试做的一个简单示例:
ui<-fluidPage(
mainPanel(
timevisOutput("gantt"),
tableOutput("return"),
actionButton("btn","btn")
)
)
server <- function(input, output, session) {
data <- data.frame(
id = 1:4,
content = c("Item one" , "Item two" ,"Ranged item", "Item four"),
start = c("2016-01-10 00:00:00", "2016-01-11T00:00:00.000Z", "2016-01-20", "2016-02-14 15:00:00"),
end = c(NA , NA, "2016-02-04", NA),
group =c(1,1,2,2)
)
output$gantt<-renderTimevis({
timevis(data= data,
groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, updateGroup=TRUE, overrideItems=TRUE), align = "left"))
})
observeEvent(input$btn,{
output$return<-renderTable(
print(input$gantt_data)
)
})
}
shinyApp(ui, server)
input$gantt_data
返回的结果是这样的:
id content start group end
1 1 Item one 2016-01-09T23:00:00.000Z 1 <NA>
2 2 Item two 2016-01-10T23:00:00.000Z 1 <NA>
3 3 Ranged item 2016-01-19T23:00:00.000Z 2 2016-02-03T23:00:00.000Z
4 4 Item four 2016-02-14T14:00:00.000Z 2 <NA>
首先:由于您使用了混合日期格式,因此第二个事件显示不正确。在下面的示例中,我更正了这一点。
由于时间线本身包含正确时区的日期,所以这只是 input$gantt_data
返回的对象的问题。您可以通过首先将其转换为 POSIXct
对象然后在您想要的时区显示它来手动更正它:
library(timevis)
ui<-fluidPage(
mainPanel(
timevisOutput("gantt"),
tableOutput("return"),
actionButton("btn","btn")
)
)
server <- function(input, output, session) {
data <- data.frame(
id = 1:4,
content = c("Item one" , "Item two" ,"Ranged item", "Item four"),
start = c("2016-01-10 00:00:00", "2016-01-11 00:00:00.00", "2016-01-20", "2016-02-14 15:00:00"),
end = c(NA , NA, "2016-02-04", NA),
group =c(1,1,2,2)
)
output$gantt<-renderTimevis({
timevis(data= data,
groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, updateGroup=TRUE, overrideItems=TRUE), align = "left"))
})
observeEvent(input$btn,{
output$return<-renderTable({
dat <- input$gantt_data
dat$start <- format(as.POSIXct(input$gantt_data$start, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"), tz="Europe/Berlin")
dat$end <- format(as.POSIXct(input$gantt_data$end, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"), tz="Europe/Berlin")
print(dat)
})
})
}
shinyApp(ui, server)
我需要取timevis返回的数据,但是返回的这些数据比输入的数据少了一小时。我也尝试过更改 R 时区和服务器,但这并没有解决问题。 这是我正在尝试做的一个简单示例:
ui<-fluidPage(
mainPanel(
timevisOutput("gantt"),
tableOutput("return"),
actionButton("btn","btn")
)
)
server <- function(input, output, session) {
data <- data.frame(
id = 1:4,
content = c("Item one" , "Item two" ,"Ranged item", "Item four"),
start = c("2016-01-10 00:00:00", "2016-01-11T00:00:00.000Z", "2016-01-20", "2016-02-14 15:00:00"),
end = c(NA , NA, "2016-02-04", NA),
group =c(1,1,2,2)
)
output$gantt<-renderTimevis({
timevis(data= data,
groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, updateGroup=TRUE, overrideItems=TRUE), align = "left"))
})
observeEvent(input$btn,{
output$return<-renderTable(
print(input$gantt_data)
)
})
}
shinyApp(ui, server)
input$gantt_data
返回的结果是这样的:
id content start group end
1 1 Item one 2016-01-09T23:00:00.000Z 1 <NA>
2 2 Item two 2016-01-10T23:00:00.000Z 1 <NA>
3 3 Ranged item 2016-01-19T23:00:00.000Z 2 2016-02-03T23:00:00.000Z
4 4 Item four 2016-02-14T14:00:00.000Z 2 <NA>
首先:由于您使用了混合日期格式,因此第二个事件显示不正确。在下面的示例中,我更正了这一点。
由于时间线本身包含正确时区的日期,所以这只是 input$gantt_data
返回的对象的问题。您可以通过首先将其转换为 POSIXct
对象然后在您想要的时区显示它来手动更正它:
library(timevis)
ui<-fluidPage(
mainPanel(
timevisOutput("gantt"),
tableOutput("return"),
actionButton("btn","btn")
)
)
server <- function(input, output, session) {
data <- data.frame(
id = 1:4,
content = c("Item one" , "Item two" ,"Ranged item", "Item four"),
start = c("2016-01-10 00:00:00", "2016-01-11 00:00:00.00", "2016-01-20", "2016-02-14 15:00:00"),
end = c(NA , NA, "2016-02-04", NA),
group =c(1,1,2,2)
)
output$gantt<-renderTimevis({
timevis(data= data,
groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, updateGroup=TRUE, overrideItems=TRUE), align = "left"))
})
observeEvent(input$btn,{
output$return<-renderTable({
dat <- input$gantt_data
dat$start <- format(as.POSIXct(input$gantt_data$start, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"), tz="Europe/Berlin")
dat$end <- format(as.POSIXct(input$gantt_data$end, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"), tz="Europe/Berlin")
print(dat)
})
})
}
shinyApp(ui, server)