我正在尝试让R暂停代码执行,让用户查看一些先前的控制台输出,以验证文件链接是否已正确匹配(使用RegEx将文件名与其各自的对象匹配).
从其他一些答案中,我想出了:
readline(prompt="Press [enter] to continue or [esc] to exit")
Run Code Online (Sandbox Code Playgroud)
作为一个独立的代码行,这可以按预期工作,但只要我在其下面添加代码,并将整个块发送到控制台,它就会直接通过readline
调用而不停止:
readline(prompt="Press [enter] to continue or [esc] to exit")
x <- 1
y <- 2
Run Code Online (Sandbox Code Playgroud)
有没有办法让R实际停在这里?
我也尝试过包装readline
函数,但它仍然不起作用
pause <- function(){
p <- readline(prompt="Press [enter] to continue or [esc] to exit")
return(p)
}
pause()
x <- 1
y <- 2
Run Code Online (Sandbox Code Playgroud)
编辑:
如果我通过脚本调用脚本source()
,readline
可以正常工作.有没有办法在不这样做的情况下获得这种行为?
我在下面的代码中最后一行的第四个问题(对不起大量的代码,我不确定需要多少)
library(mlogit)
library(foreign)
clogit <- read.table("~/R/clogit.dat", col.names=c("mode", "ttme", "invc", "invt", "gc", "chair",
"hinc", "psize", "indj", "indi", "aasc", "tasc", "basc", "casc",
"hinca", "psizea", "z", "nij", "ni"), na.strings= "-999")
clogit$mode.ids <-factor(rep(1:4,210), labels=c("air", "train", "bus", "car"))
CLOGIT <- mlogit.data(clogit,shape="long", choice="mode", alt.var="mode.ids")
res1 <- mlogit(mode~ttme+gc, data=clogit,shape="long", alt.var="mode.ids")
summary(res1)
res2 <- mlogit(mode~ttme+gc, data=CLOGIT)
summary(res2)
res3 <- mlogit(mode~ttme + gc | hinc, data=CLOGIT)
summary(res3)
res4 <- mlogit(mode~ttme | hinc | gc, data=CLOGIT)
sink("~/R/res4.txt")
cat("Here are my results:\n")
summary(res4)
sink()
Run Code Online (Sandbox Code Playgroud)
sink("~/R/res4.txt")
最后一行中的函数将存储"Here are my results"
行,但不存储文件中的 …