gpt4 book ai didi

r - 等待用户输入 5 秒,否则使用默认值

转载 作者:行者123 更新时间:2023-12-04 21:03:05 26 4
gpt4 key购买 nike

我想给用户一个输入输入的机会,如果他们在 5 秒后没有输入任何内容,则使用默认值。

这是输入部分:

input <- readline(prompt="Do something? (y/n): ")

有没有办法在R中做到这一点?

最佳答案

下面是我如何完成一个窗口提示,允许用户选择要在集群中启动的线程数。它使用默认值,然后在按下 OK 按钮或经过 5 秒后继续。

library(tcltk2)

clusterCount = 1

tklist <- list()
tklist <- within(tklist, {
# define processor input window
win1 <- tktoplevel()
rb1 <- tk2radiobutton(win1)
rb2 <- tk2radiobutton(win1)
rb3 <- tk2radiobutton(win1)
rb4 <- tk2radiobutton(win1)
rbCluster <- tclVar(clusterCount)
tkconfigure(rb1, text = "one", variable = rbCluster, value = 1L)
tkconfigure(rb2, text = "two", variable = rbCluster, value = 2L)
tkconfigure(rb3, text = "three", variable = rbCluster, value = 3L)
tkconfigure(rb4, text = "four", variable = rbCluster, value = 4L)
onOK <- function() {
clusterCount <<- as.integer(tclvalue(rbCluster))
tkdestroy(win1)
}
butOK <- tk2button(win1, text = "OK", width = -8, command = onOK)

# geometry manager
tkgrid(tk2label(win1, text = "how many cores?"), padx = 10, pady = c(15, 5))
tkgrid(rb1, padx = 10, pady = c(0, 5))
tkgrid(rb2, padx = 10, pady = c(0, 15))
tkgrid(rb3, padx = 10, pady = c(0, 15))
tkgrid(rb4, padx = 10, pady = c(0, 15))
tkgrid(butOK, padx = 10, pady = c(5, 15))
tclAfter(5000, function() tkdestroy(win1)) # delay for prompt and then close window to proceed
tkfocus(win1)
tkwait.window(win1)
})

窗口关闭后 clusterCount将保持默认值 1 或可以更改为 2、3 或 4。

关于r - 等待用户输入 5 秒,否则使用默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54112618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com