R:提供5秒钟要求暂停.如果没有要求暂停,请恢复该过程

Rem*_*i.b 11 r pausing-execution

我怎样才能为用户提供5秒的时间来写一些东西,以便要求无限期的暂停.如果在这5秒内没有要求暂停,则该过程继续.如果需要暂停,则用户具有他需要的所有时间以及他可以点击"输入"以便随时恢复该过程.

这种功能的兴趣在于如果用户不在,则暂停仅持续5秒.并且如果用户在场,则他可以享受暂停以便观看例如已经产生的图表.

代码最终可能看起来像这样:

DoYouWantaPause = function(){
   myprompt = "You have 5 seconds to write the letter <p>. If you don't the process will go on."

   foo = readline(prompt = myprompt, killAfter = 5 Seconds)    # give 5 seconds to the user. If the user enter a letter, then this letter is stored in `foo`.

   if (foo == "p" | foo == "P") {    # if the user has typed "p" or "P"
        foo = readline(prompt = "Press enter when you want to resume the process")  # Offer a pause of indefinite length
   }
}

# Main
for (i in somelist){
    ...
    DoYouWantaPause()
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*now 10

这是一个基于tcltk和tcltk2包的快速小功能:

library(tcltk)
library(tcltk2)

mywait <- function() {
    tt <- tktoplevel()
    tmp <- tclAfter(5000, function()tkdestroy(tt))
    tkpack( tkbutton(tt, text='Pause', command=function()tclAfterCancel(tmp)))
    tkpack( tkbutton(tt, text='Continue', command=function()tkdestroy(tt)),
        side='bottom')
    tkbind(tt,'<Key>', function()tkdestroy(tt) )

    tkwait.window(tt)
    invisible()
}
Run Code Online (Sandbox Code Playgroud)

运行mywait并弹出一个带有2个按钮的小窗口,如果你什么都不做,那么大约5秒后窗口就会消失并mywait返回,让R继续.如果您随时点击"继续",它将立即返回.如果您单击"暂停",则倒计时将停止,它将等待您单击继续(或按键),然后继续.

这是这里给出的答案的延伸.