timevis包中的反应性:将selectinput变量传递给子组 [英] reactivity in timevis package: passing selectinput variable to subgroup

查看:55
本文介绍了timevis包中的反应性:将selectinput变量传递给子组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中使用timevis包(dean attali),我想用r Shiny中的selectinput小部件按组分别绘制时间线:错误:无法子集不存在的列.x列 2 不存在.有人可以帮忙吗?谢谢

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

我的代码:

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组或第2组内容的时间轴吗?然后,您需要过滤您的 group 列;您不能选择 1 2 ,因为它们不存在,而1/2只是中的值组列.

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)

这篇关于timevis包中的反应性:将selectinput变量传递给子组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆