从 R 中的第一次击键开始的时间用户输入 [英] Time user input from first keystroke in R
问题描述
计算用户从第一次击键开始用 R 键入消息所需的时间.
Looking to time how long it takes a user to type a message in R from the first keystroke.
我可以使用 readline()
或 scan()
之类的函数来获取用户输入,我可以使用 system.time()
code> 来衡量代码运行需要多长时间:
I can use functions like readline()
or scan()
to get the user input, and I can use the system.time()
to measure how long it takes for the code to be run:
> system.time(readline())
Test Message
user system elapsed
0.98 0.53 19.55
但是,这些给了我总经过时间.如果我在输入前等待 15 秒,这将反映在 system.time()
的输出中,而不仅仅是我编写消息所花费的时间.(例如 19.55s = 15s + ~4.5s 我曾经输入消息.)
However, these give me the total elapsed time. If I wait wait 15 seconds before typing, this is reflected in the output of system.time()
, and not just the time I spent to write the message. (E.g 19.55s = 15s + ~4.5s I used to type the message.)
希望在控制台中发生这种情况,但也愿意在必要时使用单独的窗口.
Looking for this to happen in the console, but also willing to use a separate window if necessary.
推荐答案
我最近使用两个包 - tictoc
和 keypress
的组合解决了这个问题.
I recently solved this issue using a combination of two packages - tictoc
and keypress
.
从 keypress
您可以使用 keypress()
函数等待用户输入然后输出按下的键 - keypress()
仅在命令行中适用于 R 并且它支持大多数键,但不是全部.
From keypress
you can use the keypress()
function that waits for user input and then outputs the key that was pressed - keypress()
only works in R on command line and it supports the majority of keys, but not all of them.
从按键开始计时,您可以编写一个简单的 if
语句,从 tictoc
调用 tic()
函数.
To time from a keypress you can write a simple if
statement that calls the tic()
function from tictoc
.
示例
当我发布这个问题时,我想计算一个人在按下a"键后输入整个小写字母需要多长时间.
When I posted this question I was looking to time how long it would take someone to type the entire lowercase alphabet after hitting the 'a' key.
require(tictoc)
require(keypress)
for(i in 1:26){
a=keypress()
if(a==letters[1]) tic()
if(a==letters[26]) toc()
cat(paste(a))
}
abcdefghijklmnopqrstuvwxyz6.649 sec elapsed
当按下 'a' 时启动秒表,然后在按下 'z' 键时停止.
This starts the stopwatch when 'a' is hit, and then stops it when the 'z' key is pressed.
这篇关于从 R 中的第一次击键开始的时间用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!