2017-03-02 113 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())