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?
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 div
s and the javascript code retrieves the clients time and time zone offset and put them in the div
s.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With