4
votes

This code should show me yesterday as date for start and end with today as the last option. It worked for several month, I did not change anything, but since some weeks I get the wrong date.

It shows me the day before yesterday if I run this code locally, rstudio on my ubuntu server or connect to shiny server running this code.

Sys.setenv(TZ='GMT')

shinyApp(
  ui <- basicPage(
    dateRangeInput("daterange", "Daterange",start = Sys.Date()-1
                   ,end = Sys.Date()-1,max=Sys.Date())
  ),
  server <- function(input,output){}
)

I found this question, In Shiny for R, why does Sys.Date() return yesterday's date inside a dateInput?, and with start=NULL,end=NULL I get correctly today, but as I need yesterday this is no solution for me.

If I run Sys.Date() in the console it gives me the right date, locally and on my server. The problem only occurs if I run this as a shiny app.


Update 21.10.2015

I tried a bit around thanks to Oskar Forsmo's suggestion to get system("date") inside the app, which gives me the correct date and time.

And it gets even more strange:

library(shiny)

values <- reactiveValues()

shinyApp(
  ui <- basicPage(
    uiOutput("timerange"),
    textOutput( "today" )

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

    isolate(values$day <- Sys.Date())

    output$today <- renderText({
      as.character(values$day-1)
    })

   output$timerange <- renderUI({ dateRangeInput("daterange", "Daterange",start = values$day-1
                   ,end = values$day-1,max=values$day) })

  })

In the output object "today" I have the correct date, in the uiOutput "timerange" I have the wrong day, it shows again the day before yesterday.

I'm gonna provide a screenshot, because I would not believe myself.

wrong day in dateRangeInput, right in textOutput

And on top of that, the app run on shiny server shows the correct time/date to some of my collegues and some get the same wrong date.

To isolate the error, as I have the correct day in renderText there must be a problem somewhere with the dateRangeInput object which is triggered by my desktop?!


Update 29.10.2015

I did not change anything on the code/server but it shows now the correct date, have no idea why but it works as of today.. if anybody have an idea why this happens i would really appreciate to know.

2
have you tried setting the timezone in /usr/lib/R/etc/Renviron?RmIu
Check this Sys.timezone()Pork Chop
I set the timezone right now in /usr/lib/R/etc/Renviron/Renviron.site with the line 'TZ = "GMT" ' - I tried GMT, CET and CEST. Sys.timezone() shows the set value in RStudio, but the problem remains.Sebastian
I just checked the date on the ubuntu server, it is correctly set to CEST. I'm really confused right now, doesn't seem to be a timezone problem. I even have this behaviour on my windows desktop as stated above.Sebastian
Hmm, then I don't have a clue on what's causing this. Out of curiosity what happens if you get the date by system("date") from inside the shiny app?RmIu

2 Answers

3
votes

I had the same issue, and it was due to local timezone being different than the server timezone. I was able to get the correct date to appear when I converted each date to a POSIX date (which includes time zone). If you want shiny to display the original format created by Sys.Date(), you will also have to use the format parameter.

Here is working code:

dateRangeInput("daterange", "Daterange", start = as.POSIXct(Sys.Date()-1),
              end = as.POSIXct(Sys.Date()-1), max = as.POSIXct(Sys.Date())),
              format = 'yyyy-mm-dd')
1
votes

You need to make your UI a function. Shiny cache's the UI portion, so it is just pulling the cached Sys.Date(). See https://github.com/rstudio/shiny/issues/1882

The date you are seeing is usually the last time your server was restarted.

Copied from link above:

Turn this:

ui <- fluidPage(...)

into this:

ui <- function(req) {
  fluidPage(...)
}