Lubridate problems in R Shiny

Viewed 203

I have an issue with R shiny. I have written an R shiny application that does various things, one of the things it does is uses the Lubridate function below

df$UTCTime <- dmy_hms(df$UTCTime)

# note that this error cannot be fixed by using df[,2] instead of the "$" operator, as I have already tried this

This line of code works fine in base R, however in my R shiny application, it gives the output dates as a seemingly random sequence of numbers, like so

date <- dmy_hms(07/02/2019 22:03:21.714)  
# this gives the output date = 1549577001.71

I've recently introduced a function to my Shiny app and its caused a whole host of problems, that do not show up when you run the function in a normal R script, and I believe most of them can be traced back to this issue.

Any help with this would be greatly appreciated!

Here is a simplified version of my code, that produces the same error (there is no actual error, its just that all of the dates get converted to something like 1549577001.71).

library(shiny)
library(lubridate)

converter <- function(input_data){
    df <- input_data
    df$Date <- dmy_hms(df$Date)
    return(df)
}

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(),
        mainPanel(
            tableOutput("contents")
        )
    )
)

server <- function(input, output) {
    output$contents <- renderTable({
        Date <- c("07/02/2019 22:03:21.714")
        df <- data.frame(Date)
        converter(df)
    })
}

shinyApp(ui, server)


1 Answers

1549577001 is the internal representation of a POSIXct datetime object (number of seconds since 1970).
A solution is to output the date as character:

library(shiny)
library(lubridate)


converter <- function(input_data){
  df_ <- input_data
  df_$Date <- as.character(dmy_hms(df_$Date))
  return(df_)
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    Date <- c("07/02/2019 22:03:21.714")
    df <- data.frame(Date)
    converter(df)
  })
}

shinyApp(ui, server)

enter image description here

Related