瀏覽代碼

Calculate typing speed

shiny-input
Garrick Aden-Buie 6 年之前
父節點
當前提交
fa02cb2314
共有 3 個檔案被更改,包括 33 行新增1 行删除
  1. +2
    -0
      inst/shiny-input-app/app.R
  2. +23
    -0
      inst/shiny-input-app/dev-shiny-input.Rmd
  3. +8
    -1
      inst/shiny-input-app/typing.js

+ 2
- 0
inst/shiny-input-app/app.R 查看文件

@@ -1,5 +1,7 @@
library(shiny)

options(scipen = 1e3)

typingSpeedInput <- function(inputId, label, placeholder = NULL) {
.label <- label
htmltools::withTags(

+ 23
- 0
inst/shiny-input-app/dev-shiny-input.Rmd 查看文件

@@ -408,3 +408,26 @@ and a `null` value is returned.

Include the timing value in the data returned to Shiny
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


+ 8
- 1
inst/shiny-input-app/typing.js 查看文件

@@ -29,7 +29,14 @@ $.extend(typingSpeed, {
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) {
// This method is used for restoring the bookmarked state of your input

Loading…
取消
儲存