2013-03-07 111 views
93

我想暫停我的R腳本,直到用戶按下一個鍵。如何等待R中的按鍵?

我該怎麼做?

+0

你有沒有發現,你可以接受任何答案? – 2017-01-21 13:47:24

回答

65

正如有人在評論中寫道的,您不必在readline()之前使用貓。簡單地寫:

readline(prompt="Press [enter] to continue") 

如果你不想把它分配給一個變量,不希望打印到控制檯上一回,包裹在readline()一個invisible()

invisible(readline(prompt="Press [enter] to continue")) 
+0

我認爲這是這裏最好的答案。 – 2017-01-21 13:46:39

68

方法1

等待,直到你按[Enter]在控制檯:

cat ("Press [enter] to continue") 
line <- readline() 

包裝成一個函數:

readkey <- function() 
{ 
    cat ("Press [enter] to continue") 
    line <- readline() 
} 

此功能的Console.ReadKey()最好相當於在C#中。

方法2

暫停,直到您鍵入鍵盤上的[Enter]鍵擊。這種方法的缺點是,如果你輸入的不是數字,它會顯示一個錯誤。

print ("Press [enter] to continue") 
number <- scan(n=1) 

包裝成一個函數:

readkey <- function() 
{ 
    cat("[press [enter] to continue]") 
    number <- scan(n=1) 
} 

方法3

假如你想在圖形上繪製另一點之前等待一個按鍵。在這種情況下,我們可以使用getGraphicsEvent()等待圖形內的按鍵。

此示例程序說明了這一概念:

readkeygraph <- function(prompt) 
{ 
    getGraphicsEvent(prompt = prompt, 
       onMouseDown = NULL, onMouseMove = NULL, 
       onMouseUp = NULL, onKeybd = onKeybd, 
       consolePrompt = "[click on graph then follow top prompt to continue]") 
    Sys.sleep(0.01) 
    return(keyPressed) 
} 

onKeybd <- function(key) 
{ 
    keyPressed <<- key 
} 

xaxis=c(1:10) # Set up the x-axis. 
yaxis=runif(10,min=0,max=1) # Set up the y-axis. 
plot(xaxis,yaxis) 

for (i in xaxis) 
{ 
    # On each keypress, color the points on the graph in red, one by one. 
    points(i,yaxis[i],col="red", pch=19) 
    keyPressed = readkeygraph("[press any key to continue]") 
} 

在這裏,您可以看到圖中,其點顏色的一半,等待鍵盤上的一個按鍵。

兼容性:在環境下測試使用win.graph或X11。適用於Windows 7 x64和Revolution R v6.1。在RStudio下不起作用(因爲它不使用win.graph)。

enter image description here

+5

通過對'readline'使用'prompt'參數可以縮短方法1。方法2可以處理任何輸入(不只是數字),如果'what =「」'被添加到'scan'的調用中。 'getGraphicsEvent'只能在某些平臺上的特定圖形設備上工作(但如果您使用其中一種設備,則工作正常)。 – 2013-03-07 22:36:16

+2

'readline'和'scan'在Linux下的命令行中不起作用 – 2014-02-21 06:08:27

+2

如果您在循環中使用此函數(方法1)並想要停止循環,請包括例如: 'if(line ==「Q 「)stop()' – 2017-02-17 13:04:41

17

這裏是一個小功能(使用tcltk包),將打開一個小窗口,等待,直到您點擊繼續按鈕或按任意鍵(而小窗口仍具有焦點) ,那麼它會讓你的腳本繼續。

library(tcltk) 

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

    tkwait.window(tt) 
} 

只要把mywait()腳本中任何你想要的腳本暫停。

這適用於任何支持tcltk(我認爲都是常見的)的平臺,它可以響應任何按鍵(不僅僅是輸入),甚至可以在腳本以批處理模式運行時工作(但它仍然在批處理模式下暫停,所以如果你不在那裏繼續它將永遠等待)。如果未點擊或按下按鍵,則可以添加定時器以在設定的時間量之後繼續。

它沒有返回哪個鍵被按下(但可以修改爲這樣做)。

+0

太棒了。但出於某種原因('結構錯誤(.External(.C_dotTclObjv,objv),class =「tclObj」)): [tcl]無效的命令名稱「toplevel 「#) – milia 2016-06-18 10:09:05

+1

@ milia,這是正確的。基於tcltk的代碼需要在本地機器上運行,並且不會在RStudio-Server上運行。 – 2016-06-20 17:35:48

11

R和Rscript都將''發送到readline並以非交互模式掃描(請參閱? readline)。解決方案是使用掃描強制stdin

cat('Solution to everything? > ') 
b <- scan("stdin", character(), n=1) 

例子:

$ Rscript t.R 
Solution to everything? > 42 
Read 1 item 
+0

太棒了!這幾乎解決了[我的問題](https://stackoverflow.com/questions/47294283/how-to-run-an-r-script-and-show-a-plot)。如果控制檯沒有等待文本+返回,而是對第一個按鍵作出反應(如「按任意鍵繼續」),這仍然會很好。 – Vorac 2017-11-15 14:59:49