Time shift between local and online shiny app

1,463 views
Skip to first unread message

Aurelio García

unread,
Jan 15, 2016, 9:43:05 AM1/15/16
to Shiny - Web Framework for R
Hi,

I have developed a shiny app and I have important differences between my locale executed app in Rstudio and the app at shinyapps.io.

The app plots a graph. The x variable is a POSIXct variable with timezone = "CET". My locale = "es_ES"

When I run the locale app the plot is right. But in shinyapps.io, values are shifted one hour. So if the right plot shows a peak at 18:30, the online plot shows the peak at 17:30. All values are shifted.

I have tried to force locale option and to force timezone to "CET" (or to "GMT") but nothing worked.

Any idea?

My app also includes a reactive slider that lets the user chose the plot range, but I don't think this is the problem.

Thanks

Aurelio García

unread,
Jan 22, 2016, 8:39:26 AM1/22/16
to Shiny - Web Framework for R
Hi again,

I have not received any suggestion or clue on what my mistake is. Maybe I haven't explained my problem properly.

The fact is:

I wrote a shiny app that plots some graphs. In those graphs the X axis is 'datetime', a POXISct type variable. Timezone is set to 'CET' for all values of datetime. I am plotting another variable (power level of a signal) on the Y axis.

When I run the shiny app locally from Rstudio I get the right plot. I have uploaded the app to shinyapps.io and there the plot is displaced by -1 hour. Al values are plotted one hour before than the real value.

I have tried to force locale option and to force timezone to "CET" (or to "GMT") but nothing worked.

Any idea?

Thanks so much

Aurelio

Andy Kipp

unread,
Jan 22, 2016, 9:53:36 AM1/22/16
to Aurelio García, Shiny - Web Framework for R
Shinyapps.io uses UTC time. Time settings are not locale dependent, so setting locale won't help. Have you tried using Sys.timezone?

-Andy

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e4c7b3b4-31f6-45f5-97f7-dc9f6601bf9a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Aurelio García

unread,
Jan 22, 2016, 10:19:56 AM1/22/16
to Shiny - Web Framework for R, aurelio...@gmail.com
Hi Andy,

Thanks for that.

I have tried to force timezone using lubridate: 

plot_dataset$datetime <- force_tz(plot_dataset$datetime, tzone = Sys.timezone()) 

But didn't work. The output is the same as previous, both in local and online.

Joe Cheng

unread,
Jan 22, 2016, 10:22:55 AM1/22/16
to Aurelio García, Garrett Grolemund, Shiny - Web Framework for R

Andy Kipp

unread,
Jan 25, 2016, 3:34:12 PM1/25/16
to Joe Cheng, Aurelio García, Garrett Grolemund, Shiny - Web Framework for R
Aurelio,

I'm afraid we're outside my area of expertise here. But what you want to do seems like it should be possible.

Any else have any thoughts?

-Andy

Joe Cheng

unread,
Jan 25, 2016, 4:09:18 PM1/25/16
to Andy Kipp, Aurelio García, Garrett Grolemund, Shiny - Web Framework for R
Any chance we can get a reproducible example, Aurelio?

Aurelio García

unread,
Feb 8, 2016, 12:20:27 PM2/8/16
to Shiny - Web Framework for R, an...@rstudio.com, aurelio...@gmail.com, gar...@rstudio.com
Hi Joe,

Let's try.

This is the code in my server.R


library(shiny)
library(ggplot2)
library(lubridate)
library(tree)
library(tidyr)

# Reads data frames

sergio_train_plot <- readRDS("data/sergio_train_plot.RDS")
sergio_test_plot <- readRDS("data/sergio_test_plot.RDS")

shinyServer(function(input, output, session) {

# Choses one dataframe according to a selectInput in ui.R
 
  data_plot <- reactive({
    switch(input$user,
           "Sergio (train)" = sergio_train_plot,
           "Sergio (test)" = sergio_test_plot
                   )
  })

 # Creates a reactive slider that lets the user to choose max and min datetimes to plot 

  output$reacSlider <- renderUI({    
    mindate = min(round.POSIXt(data_plot()$datetime - 30*60, "hour"))
    maxdate = max(round.POSIXt(data_plot()$datetime + 30*60, "hour"))
    sliderInput("periodo", "Periodo", 
                min = mindate , max = maxdate, 
                value = c(mindate, maxdate), 
                step = 3600,
                width = "80%")
  })
  
  time_margins <- reactive({
    input$periodo
  })
  
  output$plot1 <- renderPlot({
    t1 <- time_margins()[1]
    t2 <- time_margins()[2]
    plot_dataset <- data_plot()
    dataset1 <- plot_dataset[plot_dataset$datetime > t1 & plot_dataset$datetime < t2,]
    ggplot(data = dataset1) + 
      geom_line(aes(x = datetime, y = rssi, colour = wp), size=.5) + 
      ylim(-100, -20) +
      theme(plot.title=element_text(vjust = -2.5)) +  
      theme(axis.title.x=element_blank(), 
            axis.title.y=element_blank()) +
      labs(colour="Wall Plug:") +
      geom_rect(aes(xmin=datetime-30, xmax=datetime+30, ymin=-100, ymax=-97, fill=factor(patron))) +
      labs(fill="Patrón:")# +      scale_fill_discrete(labels=c(1:12))
    })

  output$plot2 <- renderPlot({
    t1 <- time_margins()[1]
    t2 <- time_margins()[2]
    plot_dataset <- data_plot()
    dataset1 <- plot_dataset[plot_dataset$datetime > t1 & plot_dataset$datetime < t2,]
    ggplot(data = dataset1) + 
      geom_line(aes(x = datetime, y = rssi, colour = wp), size=.5) +
      ylim(-100, -20) +
      theme(plot.title=element_text(vjust = -2.5)) +  
      theme(axis.title.x=element_blank(), 
            axis.title.y=element_blank()) +
      labs(colour="Wall Plug:") +
      geom_rect(aes(xmin=datetime-30, xmax=datetime+30, ymin=-100, ymax=-97, fill=factor(prediccion))) +
      labs(fill="Predicción")
  })
  
})



And this is the ui.R code


library(shiny)

shinyUI(fluidPage(

  titlePanel(h1("Calibración de sensores por patrones")),

  fluidRow(
    column(12,
           h2("Control")
    ),
    
    column(2,
      selectInput("user", "Usuario", c("Sergio (train)", "Sergio (test)"))
    ),
      
    column(9, offset = 1,
      uiOutput("reacSlider")
    )
  ),
  
  fluidRow(
    column(12,
      
      h2("Resultados"),
      plotOutput("plot1"),
      plotOutput("plot2")

    )
  )
 
  
  
))

 

Thanks for your help

Joe Cheng

unread,
Feb 8, 2016, 2:23:53 PM2/8/16
to Aurelio García, Shiny - Web Framework for R, an...@rstudio.com, gar...@rstudio.com
Are the .Rds data files something you can share?

Aurelio García

unread,
Feb 8, 2016, 5:54:36 PM2/8/16
to Shiny - Web Framework for R, aurelio...@gmail.com, an...@rstudio.com, gar...@rstudio.com
There goes a simulated RDS file.

Hope you can check the problem.

It's OK when running the app locally, but the time is not correct when uploading it to Shinyapps.

Thanks!
sergio_test_plot.RDS

Phill Clarke

unread,
Feb 12, 2016, 3:23:21 AM2/12/16
to Shiny - Web Framework for R, aurelio...@gmail.com, an...@rstudio.com, gar...@rstudio.com
What about:

Sys.setenv(TZ='GMT')

Does that work perhaps?

Aurelio García

unread,
Feb 19, 2016, 4:18:57 AM2/19/16
to Phill Clarke, Shiny - Web Framework for R, an...@rstudio.com, gar...@rstudio.com
I am trying in a simpler example, but for the moment it seems that it works using:

Sys.setenv(TZ="Europe/Paris")

Thanks so much Phill!

Aurelio

Aurelio García

unread,
Feb 19, 2016, 4:20:30 AM2/19/16
to Shiny - Web Framework for R, aurelio...@gmail.com, an...@rstudio.com, gar...@rstudio.com
I am trying in a simpler example, but for the moment it seems that it works using:

Sys.setenv(TZ="Europe/Paris")

Thanks so much Phill!

Aurelio



Reply all
Reply to author
Forward
0 new messages