| library(shiny) | library(shiny) | ||||
| options(scipen = 1e3) | |||||
| typingSpeedInput <- function(inputId, label, placeholder = NULL) { | typingSpeedInput <- function(inputId, label, placeholder = NULL) { | ||||
| .label <- label | .label <- label | ||||
| htmltools::withTags( | htmltools::withTags( |
| Include the timing value in the data returned to Shiny | Include the timing value in the data returned to Shiny | ||||
| so that you can verify it's working. | so that you can verify it's working. | ||||
| (`r github_sha_link("073186bcc9741cb2494b4b9a17042ea5716a7dd5")`) | |||||
| ### Your Turn: How Fast? | |||||
| Now you're ready to calculate typing statistics. | |||||
| Here's the idea: | |||||
| each time the user presses a key, | |||||
| we calculate the `elapsed` time in seconds since they started typing. | |||||
| Then, from that value calculate: | |||||
| - words per minute (`wpm`) | |||||
| - characters per second (`cps`) | |||||
| Return an object/list with | |||||
| - `wpm`, | |||||
| - `cps`, | |||||
| - the current timestamp as `time` and | |||||
| - the `text` in the input | |||||
| return null | return null | ||||
| } | } | ||||
| return {nchar, nword, timing: this._timing}; | |||||
| let time = Date.now() | |||||
| let elapsed = (time - this._timing) / 1000 | |||||
| return { | |||||
| wpm: nword / elapsed * 60, | |||||
| cps: nchar / elapsed, | |||||
| time, | |||||
| text: el.value | |||||
| } | |||||
| }, | }, | ||||
| setValue: function(el, value) { | setValue: function(el, value) { | ||||
| // This method is used for restoring the bookmarked state of your input | // This method is used for restoring the bookmarked state of your input |