0

I'm creating a game in Shiny that needs accurate measurements of the time a user clicks on a photo (ideally precise down to the hundredth of a second, but a tenth of a second would work too).

The server time is a function of the internet connection, which can be slow with latency and what not. Therefore, I'd like to access the time on the client's machine.

I am able to retrieve the server system time at a button press. I'm also able to get the client's system time at the launch of the shiny app (using java), but I'm unable to retrieve the client's system time for each click. However, I'm getting what seems to be NA on the first click and then the time of the previous click. Any thoughts?

(The example below is based on a button click for the sake of simplicity.)

EDITED (I implemented Shiny.onInputChange and changed the time parsing)

library(shiny) 
library(shinyjs)

if (interactive()) {
  ui <- shinyUI(
    fluidPage(
      useShinyjs(),
      verbatimTextOutput("clientTime"),
      verbatimTextOutput("serverTime"),
      HTML('<input type="text" id="client_time" name="client_time" style="display: none;"> '),
      actionButton("btn1","Click to see times")


  ))

  server <- function(input, output){


    # this block fires each time we receive a message from JavaScript
    output$text <- renderText({

      req(input$count)

      paste("you clicked", input$count, "times on the RStudio ball")
    })



    observeEvent(input$btn1,
                 {
                     shinyjs::runjs(
                       '
                       $(function() {
                         var time_now = new Date();
                         Shiny.onInputChange("client_time", time_now)
                       });
                       ')
                   times <- as.POSIXct(
                     input$client_time,
                     format = "%Y-%m-%dT%H:%M:%OS")
                   times <- paste("Client Time:", format(times, "%Y-%m-%d %H:%M:%OS3"))
                   output$clientTime <- renderText(times)

                   servertimes <- Sys.time()
                   servertimes <- paste("Server Time:", format(servertimes, "%Y-%m-%d %H:%M:%OS3"))
                   output$serverTime <- renderText(servertimes)
                 })
  }
  shinyApp(ui = ui, server = server) }
dca
  • 594
  • 4
  • 18
  • Possible duplicate of https://stackoverflow.com/q/24842229. (I have personally used one of the not-accepted answers, https://stackoverflow.com/a/34221031) – r2evans Oct 09 '19 at 18:34
  • I don't think that will work because it would rely on sending a request from the server each time. Am I wrong? – dca Oct 09 '19 at 18:51
  • I didn't profile it extensively, but I don't think it's sending repeated queries. (I'd be curious to know if you find that it does, though!) – r2evans Oct 09 '19 at 20:00
  • This seems fine to me, but if you're intending to send the client time back to the server, shouldn't you be using `Shiny.onInputChange()`? – greg L Oct 10 '19 at 02:05
  • @gregL I'm not sure how to implement `Shiny.onInputChange()` even after having read about it. Can you provide an example in the context of my code? – dca Oct 10 '19 at 23:16
  • I edited the example and post to reflect some progress (one problem, I think, was I was incorrectly parsing the Java time into a r time class). – dca Oct 11 '19 at 00:04

0 Answers0