2011-04-13 79 views
4

我相信你在回归对象上执行plot命令时,都知道“命中返回以显示下一个plot”语句。我想知道如何在R中自己进行这种交互。 我在邮件列表中发现了几个帖子,但没有真正全面。它大部分处理菜单()和不同的操作系统GUI。我只是希望创造这样:(控制台)R中的用户交互?

Please enter sample size n: 
> 1000 

#execution of 
rnorm(1000) 

也许我刚才错过了文档中的某些部分,根本找不到合适的语言来谷歌...

回答

5

readLinesreadline

n <- as.integer(readline(prompt = "Please enter sample size > ")) 

稍微爱好者实现:

read_value <- function(prompt_text = "", prompt_suffix = getOption("prompt"), coerce_to = "character") 
{ 
    prompt <- paste(prompt_text, prompt_suffix) 
    as(readline(prompt), coerce_to) 
} 

read_value("Please enter sample size", coerce_to = "integer") 
+2

对于 “点击或按ENTER键翻页” 的行为,使用'devAskNewPage(TRUE)' – 2011-04-13 13:27:11

1

您可以使用readLines,但我敢肯定还有其他的方法太...

ask = function(prompt) { 
    cat(paste(prompt, ':')) 
    readLines(n=1) 
} 

n = as.integer(ask('Please enter sample size n'))