我想根据 Date_Bucket 列对 timevis 输出图进行颜色编码,任何帮助将不胜感激。
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
file$Start <- as.Date(file$Start)
file$End <- as.Date(file$End)
if (interactive())
library(shiny)
shinyApp(
ui = fluidPage(
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
))
)
observeEvent(input$btn, {
fitWindow("timeline", list(animation = TRUE))
})
}
)
}
这是实现您想要的非常简单天真的方法。根据您的用例和不同值的数量(以及它是静态数字还是动态数字),您可能想要做一些更聪明的事情,但这应该是一个好的开始。您可能还想添加更多 CSS 以使每个框的轮廓不是蓝色。基本上我在这里所做的就是使用className
timevis的参数为每个 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))
})
}
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句