R에서 키 누르기를 기다리는 방법?
사용자가 키를 누를 때까지 R 스크립트를 일시 중지하고 싶습니다.
어떻게해야합니까?
누군가 이미 의견을 작성 했으므로 이전에 고양이를 사용할 필요는 없습니다 readline()
. 간단히 쓰십시오 :
readline(prompt="Press [enter] to continue")
당신이 변수에 할당하지 않고 콘솔에 인쇄 복귀를 원하지 않는 경우, 포장 readline()
의를 invisible()
:
invisible(readline(prompt="Press [enter] to continue"))
방법 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이 사용됩니다 . Revolution R v6.1이 설치된 Windows 7 x64에서 작동합니다. RStudio에서는 작동하지 않습니다 (win.graph를 사용하지 않으므로).
작은 창을 열고 계속 버튼을 클릭하거나 아무 키나 누를 때까지 기다리는 작은 기능 (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()
스크립트를 일시 중지하려는 곳이면 어디든지 스크립트를 넣으 십시오.
This works on any platform that supports tcltk (which I think is all the common ones), will respond to any key press (not just enter), and even works when the script is run in batch mode (but it still pauses in batch mode, so if you are not there to continue it it will wait forever). A timer could be added to make it continue after a set amount of time if not clicked or has a key pressed.
It does not return which key was pressed (but could be modified to do so).
R and Rscript both send ''
to readline and scan in non-interactive mode (see ? readline
). The solution is to force stdin
using scan.
cat('Solution to everything? > ')
b <- scan("stdin", character(), n=1)
Example:
$ Rscript t.R
Solution to everything? > 42
Read 1 item
This answer is similar to that of Simon's, but does not require extra input other than a newline.
cat("Press Enter to continue...")
invisible(scan("stdin", character(), nlines = 1, quiet = TRUE))
Using nlines=1
instead of n=1
, the user can simply press enter to continue the Rscript.
참고URL : https://stackoverflow.com/questions/15272916/how-to-wait-for-a-keypress-in-r
'Programing' 카테고리의 다른 글
파이썬의 목적 __repr__ (0) | 2020.07.14 |
---|---|
인터페이스를 구현하는 추상 클래스가 왜 인터페이스 메소드 중 하나의 선언 / 구현을 놓칠 수 있습니까? (0) | 2020.07.14 |
AngularJs 앱을 작성할 때 Jade 또는 Handlebars를 사용하는 것은 무엇입니까 (0) | 2020.07.14 |
파이썬에서 문자열로 유니 코드를 선언하는 이유는 무엇입니까? (0) | 2020.07.14 |
ImportError : numpy.core.multiarray를 가져 오지 못했습니다. (0) | 2020.07.14 |