reactivity in timevis package: passing selectinput variable to subgroup

我与影子孤独终老i 提交于 2021-01-29 09:02:25

问题


Using the timevis package (dean attali) in R I would like to plot the timeline by group individually with a selectinput widget in r shiny: Error in : Can't subset columns that don't exist. x Column 2 doesn't exist. Can someone help? Thank you

My code:

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({
    select(data, all_of(c(var_group()))) %>% 
    timevis(data)
  })
}

shinyApp(ui = ui, server = server)

回答1:


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)


来源:https://stackoverflow.com/questions/64725937/reactivity-in-timevis-package-passing-selectinput-variable-to-subgroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!