R - 使用 System.time 获取当前日期,并将时间更改为 timevis 时间轴的固定值

R - Using System.time to get current date, and change time to fixed values for timevis timeline

我正在尝试使用 timevis 包创建一个交互式单日时间轴生成器。进入时间轴的每个项目都会有一个以分钟为单位的“长度”属性。

我将时间线限制在今天上午 8 点到下午 5 点之间。

我希望每个项目都在早上 8 点开始,但结束日期必须是

todayAM + length

如果需要,我可以更改长度的存储方式。

关于如何操纵时间的任何想法,以便我可以绘制基本块。 (然后用户将它们拖到时间轴上的所需位置。)

library(timevis)

today <- as.character(Sys.Date())
todayAM <- paste(today,"08:00:00")
todayPM <- paste(today, "17:00:00")

items <- data.frame(
  category = c("Room","IceBreaker","Activity","Break"),
  categoryid=c(1,2,3,4),
  name = c("Big Room","Introductions","Red Rover","Lunch"),
  length = c(480,60,120,90)
)


data <- data.frame(
  id = 1:4,
  start = c(todayAM, todayAM, todayAM, todayAM),
  end = c(todayPM, todayPM, todayPM, todayPM),
  content = items$name,
  group = items$categoryid
)

groups <- data.frame(id= items$categoryid, content = items$category)

timevis(
  data = data,
  groups = groups,
  fit = TRUE,
  options = list(editable = TRUE, multiselect = TRUE, align = "center", stack = TRUE,
                 start = todayAM,
                 end = todayPM
                 )
  
)

你可以这样做:

library(dplyr)
library(lubridate)
starthour <- 8
data <- items %>% mutate(start = as.POSIXct(Sys.Date()) + hours(starthour) + minutes(lag(cumsum(items$length),1,default=0)),
                         end   = as.POSIXct(Sys.Date()) + hours(starthour) + minutes(cumsum(items$length)),
                         content = name)

timevis::timevis(
  data = data,
  fit = TRUE,
  options = list(editable = TRUE, multiselect = TRUE, align = "center", stack = TRUE)
)

然后,您必须根据需要设置时区选项:as.POSIXct 创建一个 UTC 日期。