Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在按下某个键之前,如何暂停代码执行?_R_User Interface - Fatal编程技术网

R 在按下某个键之前,如何暂停代码执行?

R 在按下某个键之前,如何暂停代码执行?,r,user-interface,R,User Interface,我试图让R暂停代码执行,以便用户查看之前的一些控制台输出,以验证文件链接是否正确匹配(使用RegEx将文件名与各自的对象匹配) 从其他一些答案中,我得出: readline(prompt="Press [enter] to continue or [esc] to exit") 作为一个独立的代码行,它按预期工作,但只要我在下面添加代码,并将整个块发送到控制台,它就会直接通过readline调用运行,而不会停止: readline(prompt="Press [enter] to contin

我试图让R暂停代码执行,以便用户查看之前的一些控制台输出,以验证文件链接是否正确匹配(使用RegEx将文件名与各自的对象匹配)

从其他一些答案中,我得出:

readline(prompt="Press [enter] to continue or [esc] to exit")
作为一个独立的代码行,它按预期工作,但只要我在下面添加代码,并将整个块发送到控制台,它就会直接通过
readline
调用运行,而不会停止:

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

x <- 1
y <- 2
编辑:

如果我通过
source()
调用脚本,
readline
工作正常。有没有一种方法可以在不这样做的情况下获得这种行为?

通过“将整个块发送到控制台”,听起来就像是将代码复制粘贴到正在运行的R实例中

如果这样做,R将在接收到每一行后,立即按接收到的顺序运行每一行。如果任何一行试图获取输入,该输入将来自您正在复制粘贴的内容。因此,如果复制粘贴此项:

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

x <- 1
y <- 2
readline(提示=“按[enter]继续或[esc]退出”)

x使用
扫描将始终有效:

message("Press [enter] to continue or [esc] to exit")
scan(quiet = TRUE, what = "")

看看这个问题的答案。虽然最上面的一个使用了“readline”,但有许多替代策略建议:谢谢,你的黑体字似乎正好在钱上,描述了情况。只要没有其他人添加任何实质性的内容,我会在一两天内奖励赏金。这会产生与
readline
相同的条件,当发送到控制台时会添加额外的代码。以下代码行(无论是空行还是另一行代码)将作为
scan
的输入。不是在源文件时。正确,但我的问题是,是否有方法复制该行为而不使用
source()
调用文件<当通过
source()调用时,code>readline
也能正常工作
dostuff <- function(){
    readline(prompt="Press [enter] to continue or [esc] to exit")
    x <- 1
    y <- 2
}
dostuff()
message("Press [enter] to continue or [esc] to exit")
scan(quiet = TRUE, what = "")