Achint
Achint

Reputation: 33

Creating multiple timelines in single shiny app

I'm trying to create multiple timelines one after the other to be displayed in a single shiny app. For a minimal example, let's say I have a dataframe from which I create a timeline like this

library(timevis)
library(shiny)
df1 <- data.frame(start =c('2019-01-08 20:00:00','2019-01-08 20:30:00'))

ui <- fluidPage(timevisOutput("timeline"))
server <- function(input, output) {
  output$timeline <- renderTimevis(
    timevis(df1) 
  )
}
shinyApp(ui, server)
}

It will create a timeline like Basic Timeline

However, if the data is present in this format

df <- data.frame(id =c(1,2),
                 time1 =c('2019-01-08 20:00:00','2019-01-08 20:30:00'),
                 time2 =c('2019-01-08 21:00:00','2019-01-08 21:10:00'))

how do I create a shiny app like this Desired Output using timevis

Also, if before each timeline I can have a text output containing id like id =1 before timeline1, then id =2 before timeline 2, that would help distinguish which timeline is for which id.

Upvotes: 1

Views: 446

Answers (1)

norie
norie

Reputation: 9857

This will create the timelines as desired and adds a content column that will display the 'name' of each timeline as Timeline {id}.

library(timevis)
library(shiny)

df <- data.frame(id =c(1,2),
                 time1 =c('2019-01-08 20:00:00','2019-01-08 20:30:00'),
                 time2 =c('2019-01-08 21:00:00','2019-01-08 21:10:00'))


ui <- fluidPage(uiOutput("timelines"))

server <- function(input, output) {
  
  colnames(df) <- c("id", "start", "end")
  
  df$content = paste0("Timeline ", df$id)
  
  timelines <- sapply(1:nrow(df), function(i) {
    renderTimevis(timevis(df[i,]))
  })
  
  output$timelines <- renderUI(timelines)
  
}
shinyApp(ui, server)

This adds separate labels for each timeline rather than using the content column.

library(timevis)
library(shiny)

df <- data.frame(id =c(1,2),
                 time1 =c('2019-01-08 20:00:00','2019-01-08 20:30:00'),
                 time2 =c('2019-01-08 21:00:00','2019-01-08 21:10:00'))


ui <- fluidPage(uiOutput("timelines"))

server <- function(input, output) {
  
  timelines <- sapply(1:nrow(df), function(i) {
    timeline <- timevis(showZoom = FALSE, options = list(margin.axis = 100)) %>% 
      addItems(data.frame(start = c(df[i, "time1"], df[i, "time2"]),
                          content = c("Time1", "Time2"))) %>%
      centerTime(df[i, "time1"],list(animation=FALSE)) %>%
      fitWindow(list(animation=FALSE)) 
    tagList(list(renderText(paste0("Timeline ", i)), renderTimevis(timeline)))
  })
  
  output$timelines <- renderUI({
    timelines
    })
  
}
shinyApp(ui, server)

For some reason on another computer there was a problem with the date/times.

I added this and it appears to fix the problem.

  df$time1 <- as.POSIXct(df$time1,format="%Y-%m-%d %H:%M:%S")
  df$time2 <- as.POSIXct(df$time2,format="%Y-%m-%d %H:%M:%S")

enter image description here

Upvotes: 2

Related Questions