在 R 中的时间间隔后中断 readline() [英] Interrupting readline() after a time interval in R

查看:23
本文介绍了在 R 中的时间间隔后中断 readline()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一定时间后如何打破循环?我有一个从用户那里收集观察数据的功能.当记录数据时,用户应该有一个预定义的时间限制(在示例中为 30 秒).目前,如果用户输入的到达时间晚于时限结束,则该功能会中断.

How to break a loop after a certain elapsed time? I have a function that collects observational data from a user. The user should have a pre-defined time limit, when the data are recorded (30 sec in the example). At the moment, the function breaks, if the user-input arrives later than the end of the time limit.

record.events <- function(duration = 30, first.event = "a"){
    # Initial settings
    time.start <- proc.time()[3]
    events <- c(first.event, 0)

    # Timed data collection
    while(proc.time()[3] - time.start < duration){
        temp <- readline("record events...")
        events <- c(events, temp, proc.time()[3] - time.start)
    }

    # Format recorded data for post-processing
    events <- as.data.frame(matrix(events, byrow=T, ncol=2, 
        dimnames=list(NULL, c("events","stroke.time"))))
    events[,2] <- round(as.numeric(as.character(events[,2])),3)
    return(events)
}

例如给出这个结果:

  events stroke.time
1      a       0.000
2      f       2.618
3      a      23.791
4      f      24.781
5      a      33.488

最后一个事件 (a) 在时间限制后到达.SO 在 matlab 中有一个解决方案.R中有没有办法,如何在时间一到就停止等待用户输入?

The last event (a) arrived after the time limit. SO has a solution for this in matlab. Is there a way in R, how to stop waiting for the user input as soon as the time is up?

虽然函数 setTimeLimit()R.utils::withTimeout() 可以终止执行时间过长的函数(感谢 KodlCarl Witthoft科伦坡,连同这个answer),两者都不能中断 readline().withTimeout 的文档指定:

While functions setTimeLimit() and R.utils::withTimeout() can terminate execution of a function that takes too long (thanks to Kodl, Carl Witthoft and Colombo, together with this answer), neither can interrupt readline(). Documentation to withTimeout specifies:

此外,不可能使用超时中断/中断readline"提示(例如 readline() 和 readLines());直到用户完成提示后(即按 ENTER 后)才会抛出超时异常.

Furthermore, it is not possible to interrupt/break out of a "readline" prompt (e.g. readline() and readLines()) using timeouts; the timeout exception will not be thrown until after the user completes the prompt (i.e. after pressing ENTER).

用户输入超过时间限制是唯一的方法,如何停止等待readline.可以像在我的代码中一样使用 while 循环执行检查,也可以使用 setTimeLimitwithTimeouttryCatch.因此,我接受 Kodl 的回答.

The user input after the time limit is thus the only way, how to stop waiting for readline. The check can be executed with the while loop as in my code, or with setTimeLimit or withTimeout in a combination with tryCatch. I therefore accept Kodl's answer.

推荐答案

我认为您可以使用库库中的功能setTimeLimit".所以...

i think you can use fucntion "setTimeLimit" from library base. so...

record.events <- function(duration = 30, first.event = "a"){
    # Initial settings
    time.start <- proc.time()[3]
    events<-first.event
    stroke.time<-c(0)

# Timed data collection
    while(proc.time()[3] - time.start < duration){
    temp <- tryCatch({setTimeLimit(elapsed=(time.start + duration - proc.time()[3]),
                     transient = TRUE);readline("record events...")},
                     error = function(e) { return("NULL")})
    #you need to set up back this function... (but why i dont know????)
    setTimeLimit(elapsed = Inf, transient = TRUE)

     events[length(events)+1] <- temp
    stroke.time[length(stroke.time)+1]<-round(proc.time()[3],3)
}

# Format recorded data for post-processing

events<-data.frame(events, stroke.time) 
return(events)
}

但是 setTimeLimit 非常适合在用户函数中使用.我的结果是:

But setTimeLimit inst great for use in user functions.. My results is:

  events stroke.time
1      a        0.00
2      s     1539.12
3      s     1539.52
4    ass     1539.96
5      s     1540.49
6    asd     1540.94
7    fed     1541.27
8   NULL     1541.55

有关更多信息,请参阅:

For more info see:

https://stackoverflow.com/a/7891479

https://stat.ethz.ch/R-manual/R-devel/library/base/html/setTimeLimit.html

setTimeLimit 在 R 中是如何工作的?

setTimeLimit 无法终止 R 中的空闲呼叫

这篇关于在 R 中的时间间隔后中断 readline()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆