R Shiny dashboard shows incorrect datetime compared to base data

Viewed 74

R Shiny shows completely unrelated and wrong datetime compared to the baseline data: The output of baseline data:

data %>% group_by(Day = day(SampleTime), device_user) %>% 
   mutate(flag = case_when(lag(Value) != Value ~ 1, TRUE ~ 0)) %>% filter(flag == 1) %>% 
   mutate(trip_status = case_when(Value == 1 ~ 'ON', TRUE ~ 'OFF')) %>% filter(VariableDescription == 'IGNTIONSTATUS') %>% 
   select(-ThingID)
# A tibble: 6 x 7
# Groups:   Day, device_user [1]
  VariableDescription Value SampleTime          device_user   Day  flag trip_status
  <chr>               <dbl> <dttm>              <chr>       <int> <dbl> <chr>      
1 IGNTIONSTATUS           1 2020-12-27 17:25:03 BST            27     1 ON         
2 IGNTIONSTATUS           0 2020-12-27 17:35:01 BST            27     1 OFF        
3 IGNTIONSTATUS           1 2020-12-27 18:26:20 BST            27     1 ON         
4 IGNTIONSTATUS           0 2020-12-27 18:33:30 BST            27     1 OFF        
5 IGNTIONSTATUS           1 2020-12-27 18:56:48 BST            27     1 ON         
6 IGNTIONSTATUS           0 2020-12-27 19:04:06 BST            27     1 OFF 

Dashboard:

enter image description here

Server Code:

function(input, output) {

    tripsfilter <- reactive({
        
        if(input$selectaDayForTrip == 'All' & input$selectvehicleForTrip == 'All'){
            tripstatusfilterdata <- lagsdata %>% filter(VariableDescription == 'IGNTIONSTATUS')
            
        } else if(input$selectaDayForTrip == 'All' & input$selectvehicleForTrip != 'All'){
            tripstatusfilterdata <- lagsdata %>% filter(VariableDescription == 'IGNTIONSTATUS' & device_user == input$selectvehicleForTrip)
            
        } else if(input$selectaDayForTrip != 'All' & input$selectvehicleForTrip == 'All'){
            tripstatusfilterdata <- lagsdata %>% filter(VariableDescription == 'IGNTIONSTATUS' & day(SampleTime) == input$selectaDayForTrip)
            
        } else {
            tripstatusfilterdata <- tripstatusfilterdata <- lagsdata %>% filter(VariableDescription == 'IGNTIONSTATUS' & day(SampleTime) == input$selectaDayForTrip & device_user == input$selectvehicleForTrip)
        }
        return(tripstatusfilterdata)
    })
    
    output$tripStatus <- renderDT({
        datatable(data = tripsfilter()  %>% group_by(Day = day(SampleTime), device_user) %>% 
                      mutate(flag = case_when(lag(Value) != Value ~ 1, TRUE ~ 0)) %>% filter(flag == 1) %>% 
                      mutate(trip_status = case_when(Value == 1 ~ 'ON', TRUE ~ 'OFF')), options = list(pageLength = 30))
    })

}

UI Code:

dashboardPage(
    dashboardHeader(title = 'Telematics Packages'),
    dashboardSidebar(
        sidebarMenu(id = 'menu',
                    menuItem(text = 'Trip Status', tabName = 'trips_status', icon = icon('road')),
                    conditionalPanel(condition = 'input.menu == "trips_status"',
                                     selectInput('selectaDayForTrip','Select a day for trip status', choices = c('All', unique(day(lagsdata$SampleTime))), selected = max(day(lagsdata$SampleTime))),
                                     selectInput('selectvehicleForTrip','Select a Vehicle to view trip status', choices = c('All', unique(lagsdata$device_user))))
        )
    ),

dashboardBody(
    tabItems(
        tabItem(tabName = 'trips_status',
                fluidRow(
                    box(tags$style(type = "text/css", ".box-body {height:80vh}"), width = 12, solidHeader = TRUE, title = 'Trips Progress', DTOutput('tripStatus',width="100%",height="100%"))                                                
                ))
    )
)
)

Sample Data:

structure(list(VariableDescription = c("IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", "IGNTIONSTATUS", 
"IGNTIONSTATUS"), Value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0), 
    SampleTime = structure(c(1609070103, 1609070163, 1609070223, 
    1609070272, 1609070332, 1609070392, 1609070452, 1609070467, 
    1609070527, 1609070588, 1609070648, 1609070701, 1609073780, 
    1609073840, 1609073900, 1609073960, 1609074020, 1609074080, 
    1609074140, 1609074200, 1609074210, 1609075608, 1609075668, 
    1609075728, 1609075788, 1609075848, 1609075908, 1609075968, 
    1609075983, 1609075992, 1609076046), class = c("POSIXct", 
    "POSIXt"), tzone = "Asia/Kolkata"), device_user = c("BST", 
    "BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST", 
    "BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST", 
    "BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST", "BST", 
    "BST", "BST", "BST")), row.names = c(NA, -31L), class = "data.frame")

There are more than 10 devices, the amount of time difference is also not constant between devices, some are 2 hours, others are 3. So it can't be about timezone issues. Could someone help me in correcting the issue.

1 Answers

datatable converts the datetime to UTC/GMT time. If you want data to be in local time or any other timezone one way would be to convert them to character.

Compare the output of these two chunks.

library(dplyr)

df %>%
  mutate(SampleTime = as.character(SampleTime)) %>%
  DT::datatable()

enter image description here and

df %>% DT::datatable()

enter image description here

Related