Retrieving client's time zone and date

408 views
Skip to first unread message

ismart...@gmail.com

unread,
Apr 24, 2015, 10:22:18 AM4/24/15
to shiny-...@googlegroups.com
Dear all,

I'm struggling with the client's time zone retrieval. This I'd like to use it to timestamp some of the files my shiny app creates. This timestamp makes only sense with the client's data, and not with the server's.

I read that this information can be retrieved with javascript [1], but I see that the current shiny.js [2] already has some procedures to fetch this (for instance, within dateInputBindings there is a getUTCDate()).

Unfortunately I don't understand javascript and I don't see how to link this js chunk with the server.R. Any ideas of how to solve this?

Thank you in advance!

Isabel Martinez

[1] http://stackoverflow.com/questions/24842229/how-to-retrieve-the-clients-current-time-and-time-zone-when-using-shiny
[2] https://github.com/rstudio/shiny/blob/5a9c4ad8f35a42addc336b1ddf7683fa293d65a2/inst/www/shared/shiny.js

Huidong TIAN

unread,
Apr 24, 2015, 10:42:02 AM4/24/15
to shiny-...@googlegroups.com
If you don't know JavaScript, learn it. Without JavaScript you can't go very far with Shiny, I think.

To your question: 

1. create a textInput with id, e.g. #clientTime.
2. load this JavaScript in ui.R
$(document).ready(function(){
    var d = new Date();
    var target = $("#clientTime");
    target.val(d.toLocaleString());
    target.trigger("change");

});

3. add the following  in server.R
  observe({
  write( paste( Sys.time(), input$clientTime, sep = "; "), file = "Data/clientTime.txt")
  })


Tell me if it works. 

Joe Cheng

unread,
Apr 25, 2015, 5:04:23 AM4/25/15
to Huidong TIAN, shiny-...@googlegroups.com
> Without JavaScript you can't go very far with Shiny, I think.

Well I certainly hope that's not the case :-/ Though I do agree that learning JavaScript is a worthwhile investment. 

I'd replace steps 1 and 2 with:

tags$script('
$(document).ready(function(){
    var d = new Date();
    Shiny.onInputChange("clientTime", d.toLocaleString());
});
')

ismart...@gmail.com

unread,
Apr 27, 2015, 2:44:33 AM4/27/15
to shiny-...@googlegroups.com
Thank you very much.

Unfortunately when I do either of your recipes I get the value of the textInput and not the date.

Actually I have an HTML UI and therefore what I have done is:

First, to include either the Chang's or the Tian's snippet in the HTML preamble (for the sake of brevity I'll copy only one)

  <script type="text/javascript">

    $
(document).ready(function(){
   
var d = new Date();

   
Shiny.onInputChange("client_time", d.toLocaleString());
   
});
 
</script>

Second, the text input within the body

                  <input id="client_time"  type="text" value=""/>

And, third, the snippet within the server.R to generate a textfile showing the local time and the client's, as H Tian kindly suggested

    observe({
        write
( paste( Sys.time(), input$client_time, sep = "; "), file = "client_time.txt")
   
})

As I said before, I get the text I enter at the textInput but not a date. My session is as follows:

R version 3.0.2 (2013-09-25)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale
:
[1] C

attached
base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages
:
[1] shiny_0.10.2.2

loaded via a
namespace (and not attached):
[1] R6_2.0.1        RJSONIO_1.0-3   Rcpp_0.11.1     digest_0.6.4  
[5] htmltools_0.2.6 httpuv_1.3.0    mime_0.2        xtable_1.7-3

Obviously I'm missing a key point in here...

Thank you again! Bests,


Isabel Martinez


Huidong TIAN

unread,
Apr 27, 2015, 3:35:08 AM4/27/15
to shiny-...@googlegroups.com
run the following code: 
library(shiny)
runApp(list(
  ui = bootstrapPage(
        tags$script('
                     $(document).ready(function(){
                      var d = new Date();
                      var target = $("#clientTime");
                      target.val(d.toLocaleString());
                      target.trigger("change");
                      });
                    '),
    textInput("clientTime", "Client Time", value = "")
  ),
  server = function(input, output) {
    observe({
      write( paste( Sys.time(), input$clientTime, sep = "; "), file = "Data/clientTime.txt")
    })
  }
))

ismart...@gmail.com

unread,
Apr 27, 2015, 7:11:37 AM4/27/15
to shiny-...@googlegroups.com
Thank you, now I fixed it!

Seems that I misplaced the javascript chunk: when located the last in the HTML's head all works smoothly.
Reply all
Reply to author
Forward
0 new messages