8

I am building a shiny app and hosting it on the server. There are date and time inputs. The default value is Sys.Date(). Now when the user accesses it, the default value is taken as server date and time rather than the users.

Please tell me how can I get the user's current time and date and use them as default values into my input box.

Current input scenario:

dateInput("dateto", "Date To:", format = "mm/dd/yyyy", value = Sys.time()),
textInput("dateto_hour", "HH:MM",
                value = gsub("(.*):.*","\\1",format(Sys.time(), "%X")))
Rohit Kumar Singh
  • 647
  • 1
  • 7
  • 17
  • 2
    Look into adding the `session` argument to your server. There are variables there that may give you user data. – Ryan Morton May 17 '18 at 16:03
  • I checked the sessionInfo() in the console and found the information about the R version, libraries. But sadly no information about the session. – Rohit Kumar Singh May 17 '18 at 16:34
  • You need to look at `session` not `sessionInfo`: https://shiny.rstudio.com/reference/shiny/1.0.2/session.html – Ryan Morton May 17 '18 at 16:41

1 Answers1

6

There are a couple solutions that people have found to this (for example, here), and they all have particular advantages. Since you're looking to use it as the default value in a textInput, this solution that I've adopted for a similar need may work well for you. It involves reading the client's browser time using some JS, assigning that as the default value in a textInput, and then using that textInput later in the server. In my application, I'm using this to timestamp data submissions from the user.

In the UI, you need the follow JS script just before your textInput:

tags$script('
          $(document).ready(function(){
          var d = new Date();
          var target = $("#clientTime");
          target.val(d.toLocaleString());
          target.trigger("change");
          });
          '),
textInput("clientTime", "Client Time", value = "")

As suggested in the comments, session$userData can be used to store session-specific data such as input$clientTime for use and manipulation in the server. Below is a complete app showing the difference between the server time and the client time, but you'll obviously need to publish it to a server in order to see the difference.

library(shiny)

ui <- fluidPage(
  verbatimTextOutput("server"),
  tags$script('
          $(document).ready(function(){
          var d = new Date();
          var target = $("#clientTime");
          target.val(d.toLocaleString());
          target.trigger("change");
          });
          '),
  textInput("clientTime", "Client Time", value = ""),
  verbatimTextOutput("local")
)

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

  output$server <- renderText({ c("Server time:", as.character(Sys.time()), as.character(Sys.timezone())) })
  session$userData$time <- reactive({format(lubridate::mdy_hms(as.character(input$clientTime)), "%d/%m/%Y; %H:%M:%S")})
  output$local <- renderText({session$userData$time() })


}

shinyApp(ui = ui, server = server)
phalteman
  • 3,442
  • 1
  • 29
  • 46
  • 1
    Thanks @phalteman !! It worked perfectly for me, and I finally get a way to change the default values of some of inputs in my app, each time a user refreshes the browser. How can I extract the date for example in "dd/mm/yyyy" format? – Elias Mar 01 '19 at 13:44
  • 1
    I tried something like this but it didn't work as a default value for a dateInput: tags$script('$(document).ready(function(){var d = new Date();var year =d.getFullYear();var month = d.getMonth();var date = d.getDate();var dateString = year + "-" + (month + 1) + "-" + date; var target = $("#mydate");target.val(dateString);target.trigger("change");}); '), – Elias Mar 01 '19 at 13:53
  • I don't know enough JS to know how to change the format of the client time in the text input box. However, you can store and display the time in whatever format you want using `format()` in the reactive assignment of the time to `userData$time`. See edit above. – phalteman Mar 01 '19 at 22:50