在 R 中使用 timevis 包进行时间转换

time conversion in R using the timevis package

提问人:Haribo 提问时间:7/25/2023 更新时间:7/26/2023 访问量:43

问:

在下面,我有一个闪亮的应用程序,它使用包将项目添加到时间线。问题是当用户添加第一项时,一切都很好,一旦用户尝试添加更多项目,日期就会表现得很尴尬!它忽略了提供的日期,并始终将新项目放在 1970 年!任何如何解决这个问题的想法:timevis

library(shiny)
library(timevis)
library(dplyr)

randomID <- function() {
  paste(sample(c(letters, LETTERS, 0:9), 16, replace = TRUE), collapse = "")
}

prettyDate <- function(d) {
  if (is.null(d)) return()
  posix <- as.POSIXct(d, format = "%Y-%m-%dT%H:%M:%OS", tz = "UTC")
  corrected <- lubridate::with_tz(posix, tzone = Sys.timezone())
  format(corrected, "%Y-%m-%d %H:%M:%OS %Z")
}

ui <- fluidPage(
  timevisOutput("timeline"),
  textInput("addText", tags$h4("Add item:"), "New item"),
  dateInput("addStart", "Start date:", Sys.Date()),
  textInput("addGroup", "Group:", "gym"),
  actionButton("addBtn", "Add"),
  uiOutput("removeIdsOutput", inline = TRUE),
  actionButton("removeItem", "Remove"),
  tableOutput("table")
)

server <- function(input, output, session) {
  
  timelineData <- reactiveVal(data.frame(id = character(), 
                                         content = character(),
                                         start = character(),
                                         group = character(),
                                         type = character(), 
                                         stringsAsFactors = FALSE))
  
  observeEvent(input$addBtn, {
    newItem <- data.frame(id = randomID(),
                          content = input$addText,
                          start = as.POSIXct(input$addStart, tz = "UTC"),
                          group = input$addGroup,
                          
                          stringsAsFactors = FALSE)
    timelineData(rbind(timelineData(), newItem))
  })
  
  output$timeline <- renderTimevis({
    # Creating the groups data.frame
    groupList <- unique(timelineData()$group)
    groupData <- data.frame(id = groupList, content = groupList)
    
    timevis(data = timelineData(), groups = groupData, options = list(editable = TRUE))
  })
  
  output$table <- renderTable({
    data <- timelineData()
    data$start <- prettyDate(data$start)
    data
  })
  
  output$removeIdsOutput <- renderUI({
    if(nrow(timelineData()) > 0){
      selectInput("removeIds", tags$h4("Remove item"), timelineData()$id)
    }else{
      NULL
    }
  })
  
  observeEvent(input$removeItem, {
    if(!is.null(input$removeIds)){
      timelineData(timelineData() %>%
                     filter(id != input$removeIds))
    }
  })
  
  observe({
    timelineData(input$timeline_data)
  })
  
}

shinyApp(ui = ui, server = server)
r 闪亮的 posix utc posixct

评论


答:

1赞 Jan 7/26/2023 #1

您正在定义中设置 。但是,您还应该将读取的项目转换为:start = character()timelineDatacharacter

newItem <- data.frame(id = randomID(),
                      content = input$addText,
                      start = as.character(input$addStart),
                      group = input$addGroup,
                      stringsAsFactors = FALSE)

enter image description here