Search code examples
rvis.js-timeline

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


I am attempting to use the timevis package to create an interactive single day timeline builder. Each item that goes onto the timeline will have a "length" attribute in minutes.

I am limiting the timeline to todays date between 8AM and 5PM.

I want each item to start off at 8AM, but the end date needs to be

todayAM + length

I can alter how the lengths are stored if needed.

Any ideas on how to manipulate the times so that I can get the basic blocks plotted. (The user will then drag them to desired places on the timeline.)

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
                 )
  
)

Solution

  • You could do this :

    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)
    )
    

    enter image description here You'll then have to set timezone option according to your needs : as.POSIXct create a UTC date.