How can I resolve the timezones issue on timevis R?

Viewed 123

I need to take the data returned from timevis, but these data are returned with one hour less than the data entered. I have tried changing the R time zone and the server too, but that has not solved the problem. here's a simple example of what I'm trying to do:

 ui<-fluidPage(

      mainPanel(
      timevisOutput("gantt"),
      tableOutput("return"),
      actionButton("btn","btn")
      )
    )

  server <- function(input, output, session) {



       data <- data.frame(
         id      = 1:4,
         content = c("Item one"  , "Item two"  ,"Ranged item", "Item four"),
         start   = c("2016-01-10 00:00:00", "2016-01-11T00:00:00.000Z", "2016-01-20", "2016-02-14 15:00:00"),
         end     = c(NA          ,           NA, "2016-02-04", NA),
         group   =c(1,1,2,2)
       )




      output$gantt<-renderTimevis({

        timevis(data= data,
               groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
               options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, updateGroup=TRUE, overrideItems=TRUE),  align = "left"))

      })

      observeEvent(input$btn,{
        output$return<-renderTable(
              print(input$gantt_data)
        )
      })
    }


    shinyApp(ui, server)

The result of input$gantt_data returned is this:

   id     content                    start group                      end
 1  1    Item one 2016-01-09T23:00:00.000Z     1                     <NA>
 2  2    Item two 2016-01-10T23:00:00.000Z     1                     <NA>
 3  3 Ranged item 2016-01-19T23:00:00.000Z     2 2016-02-03T23:00:00.000Z
 4  4   Item four 2016-02-14T14:00:00.000Z     2                     <NA>
1 Answers

First of all: Since you use mixed date formats, the second event is not displayed correctly. In my example below, I corrected that.

Since the timeline itself contains the dates in the correct time zone, it's just an issue of the object returned by input$gantt_data. You can manually correct that by first converting it to a POSIXct object and then displaying it in your desired timezone:

library(timevis)
ui<-fluidPage(

  mainPanel(
    timevisOutput("gantt"),
    tableOutput("return"),
    actionButton("btn","btn")
  )
)

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


  output$gantt<-renderTimevis({
    timevis(data= data,
            groups = data.frame(id = 1:4, content = c(" 1", " 2", " 3", " 4")),
            options = list(editable = list(add=FALSE, remove=TRUE, updateTime= TRUE, updateGroup=TRUE, overrideItems=TRUE),  align = "left"))

  })

  observeEvent(input$btn,{
    output$return<-renderTable({

      dat <- input$gantt_data
      dat$start <- format(as.POSIXct(input$gantt_data$start, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"), tz="Europe/Berlin")
      dat$end <-   format(as.POSIXct(input$gantt_data$end, format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"), tz="Europe/Berlin")
      print(dat)

    })
  })
}

shinyApp(ui, server)

enter image description here

Related