如何在R Shiny中为timevis绘图着色

时间:2019-02-22 11:43:39

标签: r shiny

我想根据Date_Bucket列对timevis输出图进行颜色编码,任何帮助将不胜感激。

'views': [(self.env.ref('account.invoice_tree').id, 'tree'),
          (self.env.ref('account.invoice_form').id, 'form')],

head(file)
Record_ID	Start	End	Date_Bucket
1	01-01-2017	31-12-2021	Greater than 2 Years
2	01-11-2013	31-10-2028	Greater than 2 Years
3	01-11-2017	31-10-2022	Greater than 2 Years
4	22-04-2014	30-09-2020	1-2 Years
5	15-12-2017	30-06-2019	0-6 Months
6	01-11-2017	31-10-2022	Greater than 2 Years
7	22-04-2014	30-09-2020	1-2 Years
8	11-01-2013	31-08-2019	6-12 Months
9	11-10-2013	31-08-2019	6-12 Months

enter image description here

1 个答案:

答案 0 :(得分:0)

这是一种非常简单的幼稚方式来实现您想要的。根据您的用例和不同值的数量(以及它是静态数还是动态数),您可能想做一些更聪明的事情,但这应该是一个好的开始。您可能还想添加更多CSS,以使每个框的轮廓都不是蓝色的。基本上,我在这里要做的就是使用timevis的className参数为每个Date_Bucket分配一个不同的类,并为每个这些类添加CSS。

library(shiny)
library(timevis)

file$Start <- as.Date(file$Start)
file$End <- as.Date(file$End)
cols <- c("redBg", "blueBg", "greenBg", "orangeBg")
file$className <- cols[file$Date_Bucket]

shinyApp(
  ui = fluidPage(
    tags$style(
      ".redBg { background: red; }
      .blueBg { background: blue; }
      .greenBg { background: green; }
      .orangeBg { background: orange; }"
    ),
    timevisOutput("timeline"),
    actionButton("btn", "Fit all items")
  ),
  server = function(input, output) {
    output$timeline <- renderTimevis(
      timevis(data.frame(
        id = file$Record_ID, start =  file$Start , end = file$End, content = 
          file$Date_Bucket, className = file$className
      ))
    )
    observeEvent(input$btn, {
      fitWindow("timeline", list(animation = TRUE))
    })
  }
)