reactivity in timevis package: passing selectinput variable to subgroup

So you want to either show a timeline with the contents of group 1 or 2? Then you need to filter your group column; you can’t select the columns 1 or 2 because they don’t exist, 1/2 are just the values within the group column.

library(shiny)
library(timevis)
library(dplyr)



# constructing data frame
pre_data <- data.frame(
  group      = as.integer(c(1,1,2,2)),
  content = c("Item one", "Item two",
              "Ranged item", "Ranged item two"),
  start   = c("2016-01-10", "2016-01-11",
              "2016-01-20", "2016-02-14"),
  end     = c(NA, NA, "2016-02-04", "2016-05-14")
)

# preparing for timevis
data <- pre_data %>% 
  arrange(group, start) %>% 
  group_by(group) %>% 
  mutate(id = row_number())


ui <- fluidPage(
  # selectinput getting data from dataframe
  selectInput(inputId = "group", 
              label = "group", 
              choices = data$group),
  timevisOutput("timeline")
)

server <- function(input, output, session) {
  
  # reactive input variable
  var_group <- reactive({input$group})
  
  # 
  output$timeline <- renderTimevis({
    req(var_group())
    data %>% 
      filter(group == as.integer(var_group())) %>% 
      timevis()
  })
}

shinyApp(ui = ui, server = server)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top