Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the client's current time and time zone when using Shiny?

Tags:

timezone

r

shiny

I'm wondering if there is some clever way of getting the clients current time and time zone in order to use it in the server.R part of a Shiny application. If not, what could be the easiest way of doing this?

like image 690
Rasmus Bååth Avatar asked Jul 19 '14 15:07

Rasmus Bååth


2 Answers

I found out a method that works and which is just a small modification of the stackoverflow answer to Reading javascript variable into shiny/R on app load. It does not retrieve the actuall time zone, but well the time zone offset.

In ui.R

HTML('<input type="text" id="client_time" name="client_time" style="display: none;"> '),
HTML('<input type="text" id="client_time_zone_offset" name="client_time_zone_offset" style="display: none;"> '),

tags$script('
  $(function() {
    var time_now = new Date()
    $("input#client_time").val(time_now.getTime())
    $("input#client_time_zone_offset").val(time_now.getTimezoneOffset())
  });    
')

This above created two divs and the javascript code retrieves the clients time and time zone offset and put them in the divs.

In server.R

client_time <- reactive(as.numeric(input$client_time) / 1000) # in s
time_zone_offset <- reactive(as.numeric(input$client_time_zone_offset) * 60 ) # in s 

This above creates two reactive variables that can be used in the server code. For ease of handling I also transform input$client_time from ms to s and input$client_time_zone_offset from min to s.

like image 73
Rasmus Bååth Avatar answered Oct 28 '22 05:10

Rasmus Bååth


I hadn't heard of Shiny until your post. Reading through the documentation, it would appear that the client-side portion of a Shiny application is written in R, but then renders as HTML/CSS/JavaScript so it can run in the browser. The information you're asking for would have to be sourced from JavaScript.

Getting the current time in JavaScript is quite simple:

var now = new Date();

The result is a Date object that has the current date and time from the client's clock. Internally, it's tracked as the UTC time in milliseconds since Midnight 1/1/1970 UTC. However the Date object will take the client's local time zone into account when producing output such as with .toString() or when using many of the other functions. You can read more about the Date object in the MDN reference documentation.

Now, if you actually need the time zone of the client, that's a different story. The Date object can only give you the time zone offset of a particular date and time, using the .getTimezoneOffset() function. For example, you can tell that the client is currently 420 minutes behind UTC, (UTC-07:00), but you cannot tell that the client is in the America/Los_Angeles time zone - which alternates between UTC-07:00 and UTC-08:00 for daylight saving time. Read more in the timezone tag wiki.

There is one JavaScript library, jsTimeZoneDetect, that attempts to guess at the time zone, and it does a reasonably decent job.

So - now the question would be, how do you call custom JavaScript from a Shiny app in R? I'm no expert in this area, but it would appear to be covered by this part of the Shiny documentation.

All of this would be done client-side. You would then have to send it to the server to use the information in the server.R part of the application.

like image 1
Matt Johnson-Pint Avatar answered Oct 28 '22 06:10

Matt Johnson-Pint