从R中的键盘读取输入

从R中的键盘读取输入,r,R,我知道这是一个非常基本的问题,因为我是新手,我有这个问题 如何允许用户从键盘输入数字。 为用户提供输入他们想从键盘输入多少数字的功能,并根据该功能提供输入数字的功能 例如: How many numbers you want to enter? > 10 Enter numbers: > 5 10 15 20 25 30 35 40 45 50 while(T){ num我创建了一个函数,该函数将询问用户要输入多少个数字,并根据该计数提供输入整数的便利 readnumb

我知道这是一个非常基本的问题,因为我是新手,我有这个问题

如何允许用户从键盘输入数字。 为用户提供输入他们想从键盘输入多少数字的功能,并根据该功能提供输入数字的功能

例如:

How many numbers you want to enter?  
> 10  
Enter numbers:  
> 5 10 15 20 25 30 35 40 45 50
while(T){

num我创建了一个函数,该函数将询问用户要输入多少个数字,并根据该计数提供输入整数的便利

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()) 

readnumber正如其他人已经回答的那样,
readline
是回答您问题的函数。关于您的示例,这里尝试重现所请求的行为

N <- as.numeric(readline("How many number do you want to enter? "))
x <- vector()  # making sure x is empty

for (n in seq_len(N)) {
    new_x <- readline(paste0("Enter number ", n, " of ", N, ": "))
    x <- append(x, as.numeric(new_x))
}

cat("Here are the entered numbers:\n")
print(x)

<代码> > hello >代码> > Read Load 第二个.S.Brunel'的评论,看看感谢@ RistLin…但是它不提供数字的验证..考虑到用户为第一个问题提供5的输入,但是它允许用户输入以上代码的5个数字或少于5个数字.也许问题是你不是CURA关于你的预期结果。
N <- as.numeric(readline("How many number do you want to enter? "))
x <- vector()  # making sure x is empty

for (n in seq_len(N)) {
    new_x <- readline(paste0("Enter number ", n, " of ", N, ": "))
    x <- append(x, as.numeric(new_x))
}

cat("Here are the entered numbers:\n")
print(x)