daattali / timevis

📅 Create interactive timeline visualizations in R
http://daattali.com/shiny/timevis-demo/
Other
655 stars 157 forks source link

How to format dates in the data retrieved from the widget? #138

Closed rrodrigueznt closed 1 year ago

rrodrigueznt commented 1 year ago

Hi! Can each item's start and end dates have a different format when retrieved from the widget? Based on some examples on this site, I'm playing with the code below, but I need help finding the way to format the start and end as, for instance, yyyy-mm-dd.

I get:

timevis_long

I want to get:

timevis_long
library(shiny)
library(timevis)
library(lubridate)
library(dplyr)

topleftcoor_window <- NULL

ui <- fluidPage(
    # front end interface
    timevisOutput("timeline01"),
    tableOutput("data"),
    verbatimTextOutput("data_s"),
    textOutput("ids"),
    textOutput("selected"),
    textOutput("window"),
    verbatimTextOutput("window_tl"),
    textOutput("window_tl_f"),
    textOutput("visible")
)

server <- function(input, output, session) {
    # back end logic
    data <- data.frame(
        id = 1:4,
        content = c("Item one", "Item two", "Ranged item", "Item four"),
        start = c(
            "2016-01-10",
            "2016-01-11",
            "2016-01-20",
            "2016-02-14 15:00:00"
        ),
        end = c(NA, NA, "2016-02-04", NA)
    )

    output$timeline01 <- renderTimevis(timevis(data,
        options = list(
            stack = TRUE,
            editable = TRUE,
            multiselect = TRUE,
            align = "center"
        )
    ) %>% setSelection(2))

    output$data <- renderTable(input$timeline01_data)
    output$data_s <- renderPrint(str(input$timeline01_data[c(3)]))
    output$ids <- renderText(input$timeline01_ids)
    output$selected <- renderText(input$timeline01_selected)
    output$window <- renderText(input$timeline01_window)
    output$window_tl <- renderPrint(str(input$timeline01_window[1]))
    output$window_tl_f <- renderText(
        lubridate::date(as.Date(input$timeline01_window[1]))
    )
    output$visible <- renderText(input$timeline01_visible)
}

shinyApp(ui, server)

Thanks!

daattali commented 1 year ago

You can't ask timevis to return items in a specific format, but the string you get back can be parsed with {lubridate} (or in other ways). This question is out of scope for {timevis}, it's a general question in R about "how can I convert a datetime string to a different format?" - please use a more appropriate channel to find the answer to that question.

daattali commented 1 year ago

I googled it for you and it looks like this will work: format(lubridate::as_datetime(input$timeline01_window[1]), "%Y-%m-%d")