R语言timevis包的学习
生活随笔
收集整理的這篇文章主要介紹了
R语言timevis包的学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
timevis包可以實現時間線的可視化,并支持交互。更好的是,也可以在shiny和Rmarkdown中使用!
此外此外,還有眾多的API,可以在創建后修改。支持從外部獲取數據。返回的是htmlwidgets對象。
基本用法
timevis(data, groups, showZoom = TRUE, zoomFactor = 0.5, fit = TRUE,options, width = NULL, height = NULL, elementId = NULL)?data? 包括時間線各項的數據框,每行為一個時間線。包括以下各項:
- start? (必須)item開始的日期,如"1988-11-22" or "1988-11-22 16:30:00"
- content? (必須)item的內容,可以是文本或者HTML代碼
- end? (可選)item結束的日期,如有,則此item是一個范圍,如無,則此item為一個點
- id? (可選)item的id,建議使用,因為在查詢item時會用到
- type? (可選)item的類型,默認'box',還有'point','range','background'。前兩者只需開始時間,而后兩者需要開始和結束時間
- title? (可選)item的標題,當鼠標移到它們上時顯示,僅支持文本
- editable? (可選)若為TRUE,此item能被鼠標操縱,鼠標單擊開始或結束日期時,會出現刪除符號,可刪除;雙擊一個日期時,會在此添加一個點狀的item
- group? (可選)組的id,當設置時,同一組的item將在同一個時間線上。一個豎直的軸展示各組的名字
- subgroup? (可選)子組的id,將所有項目分組的每個子組中的item,放在相同的高度上,而不是將它們堆疊在一起。
- className? (可選)用于指定類名,從而為item設置css樣式
- style? (可選)css樣式設置,例如 color: red;.
這些參數中,如果某個參數只用于部分行,則可用NA補齊;此外,這些參數還可以用setItems函數來指定
?groups? (可選)包含組數據的數據框,同一組的item將在同一個時間線上。一個豎直的軸展示各組的名字,其包括以下各項:
- id? (必須)組的id
- content? (必須)組的內容,可以是文本或者HTML代碼
- title? 組的標題,當鼠標移到它們上時顯示,僅支持文本
- subgroupOrder??按字段名稱排序子組,默認地,組通過first-come, first-show排序
- className,style 使用方法與上面data中同
?showZoom? (TRUE)包含放大/縮小的按鈕
?zoomFactor? 放大因子。例如放大因子為0.5,起初顯示20天,放大一下將顯示30天,再放大一下將顯示45天
?fit? (TRUE)當時間線初始化時,匹配時間線上的所有數據;否則,時間線將展示當前日期
?options? 配置項的列表,可用的配置項見 點擊
?
實例
啊,講了這么多,來個小實例吧。這些實例都是來自timesvis的說明文檔
#----------------------- Minimal data ----------------- timevis(data.frame(id = 1:2,content = c("one", "two"),start = c("2016-01-10", "2016-01-12")) )#----------------------- Hide the zoom buttons, allow items to be editable ----------------- timevis(data.frame(id = 1:2,content = c("one", "two"),start = c("2016-01-10", "2016-01-12")),showZoom = FALSE,options = list(editable = TRUE, height = "200px") )#----------------------- You can use %>% pipes to create timevis pipelines ----------------- timevis() %>%setItems(data.frame(id = 1:2,content = c("one", "two"),start = c("2016-01-10", "2016-01-12"))) %>%setOptions(list(editable = TRUE)) %>%addItem(list(id = 3, content = "three", start = "2016-01-11")) %>%setSelection("3") %>%fitWindow(list(animation = FALSE))#------- Items can be a single point or a range, and can contain HTML ------- timevis(data.frame(id = 1:2,content = c("one", "two<br><h3>HTML is supported</h3>"),start = c("2016-01-10", "2016-01-18"),end = c("2016-01-14", NA),style = c(NA, "color: red;")) )#----------------------- Alternative look for each item ----------------- timevis(data.frame(id = 1:2,content = c("one", "two"),start = c("2016-01-10", "2016-01-14"),end = c(NA, "2016-01-18"),type = c("point", "background")) )#----------------------- Using a function in the configuration options ----------------- timevis(data.frame(id = 1,content = "double click anywhere<br>in the timeline<br>to add an item",start = "2016-01-01"),options = list(editable = TRUE,onAdd = htmlwidgets::JS('function(item, callback) {item.content = "Hello!<br/>" + item.content;callback(item);}')) )#----------------------- Using groups ----------------- timevis(data = data.frame(start = c(Sys.Date(), Sys.Date(), Sys.Date() + 1, Sys.Date() + 2),content = c("one", "two", "three", "four"),group = c(1, 2, 1, 2)),groups = data.frame(id = 1:2, content = c("G1", "G2")))#----------------------- Getting data out of the timeline into Shiny ----------------- if (interactive()) { library(shiny)data <- data.frame(id = 1:3,start = c("2015-04-04", "2015-04-05 11:00:00", "2015-04-06 15:00:00"),end = c("2015-04-08", NA, NA),content = c("<h2>Vacation!!!</h2>", "Acupuncture", "Massage"),style = c("color: red;", NA, NA) )ui <- fluidPage(timevisOutput("appts"),div("Selected items:", textOutput("selected", inline = TRUE)),div("Visible window:", textOutput("window", inline = TRUE)),tableOutput("table") )server <- function(input, output) {output$appts <- renderTimevis(timevis(data,options = list(editable = TRUE, multiselect = TRUE, align = "center")))output$selected <- renderText(paste(input$appts_selected, collapse = " "))output$window <- renderText(paste(input$appts_window[1], "to", input$appts_window[2]))output$table <- renderTable(input$appts_data) } shinyApp(ui, server) }?
更多的shiny關于timevis的應用見此?點擊
參考:timevis包timevis函數的說明文檔。
轉載于:https://www.cnblogs.com/Hyacinth-Yuan/p/8343425.html
總結
以上是生活随笔為你收集整理的R语言timevis包的学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理解矩阵与线性代数
- 下一篇: python3.6升级及setuptoo