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?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
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
This above created two
div
s and the javascript code retrieves the clients time and time zone offset and put them in thediv
s.In server.R
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 andinput$client_time_zone_offset
from min to s.You could fetch it over socket communication, but with much overhead, or if your time between the client is synchronized, you could trust your local time, fetch their timezone, evaluate the timezone difference between the server and the client, plus/minus the difference, then you would fetch their time and timezone without much work. (Somehow probably but in most case, by doing this, would lose a precision of 2 to 3 seconds due to computer keep up)
Another way to do it : send a message to the client at any time and retrieve the result with the input object. You will also measuring latency. (Or if the user changed of time zone during the shiny session...).
Get server time and time zone, send it to client via sendCustomMessage.
Put this function in server file.
In javascript, get the message, retrieve time and zone offset, send it back to server as a new input entry :
Put this code in a script tag or in a separate js file.
In a shiny session :
It should output something like:
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:
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 theDate
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 theDate
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 theAmerica/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.