2017-03-02 116 views
2

我知道这是非常基本的问题,因为我是R新手我有这个问题。从R键盘读取输入

如何让用户从键盘输入数字。 提供用户输入他们想从键盘输入多少个号码,并根据提供的设施输入号码。

如:

How many numbers you want to enter? 
> 10 
Enter numbers: 
> 5 10 15 20 25 30 35 40 45 50 
+1

hello check'?readline' –

+0

第二@ s.brunel的评论,请看[readline的文档](https://stat.ethz.ch/R-manual/R-devel/library/base /html/readline.html) –

回答

0
while(T) { 
    num <- readline("How many number do you want to enter? > ") 
    num <- as.numeric(num) 
    if (!is.na(num)) { 
     num2 <- readline(paste0("Enter ",num, " numbers > ")) 
     print(num2) 
     break 
    } 
    } 
+0

谢谢@ raistlin ..但它不提供数字验证..考虑用户提供输入为第一个问题的5,但它允许用户输入超过5个数字为上述代码或少于5个数字 – Sriharsha

+0

也许问题是你不清楚你的预期结果。 –

0

我已经创建了一个将询问用户他们多少个数字要输入功能,并基于该指望它提供的设施,进入整数

readnumber <- function() 
{ 
    n <- readline(prompt="How many numbers do you want to enter: ") 
    n <- as.integer(n) 
    if (is.na(n)){ 
     n <- readnumber() 
    } 
    Numbers<-c() 
    for (i in 1:n){ 
     num <- readline(prompt="Enter an integer: ") 
     Numbers[i]<-as.numeric(num) 
    } 
    return(Numbers)  
} 
print(readnumber())