Wait for user input from keyboard in R before next

2020-02-15 05:07发布

I want the user to choose data before analyzing the data but I can't get the code right to wait for user input…

check<-0
count<-0
data.choice<-function(){
while(check < 1 ){
  check <-as.numeric(readline('Choose 1 or 2 for analysing Data1 or Data2 respectively:  \n 1. "Data1" \n 2. "Data2" '));
  check <- ifelse(grepl("\\D",check),-1,as.integer(check))
  if(is.na(check)|check>2|count>3){
    count<-count+1
    print('Please type only 1 or 2...')
    break}  # breaks when hit enter
}
}
data.choice()

At this point it should display

Choose 1 or 2 for analysing cells or exosomes respectively:
1. Data1

  1. Data2

and WAIT FOR USER INPUT!!!

It works if i just run the piece of code above. But if i run the whole thing it JUMPS TO THE NEXT LINE of code, for instance...:

if(check==1){   
print("Checking Data1")   
data1<-read.csv("file1.csv")
} 
else if(check==2){   
data1<-read.csv("file2.csv") 
}

I have read these other posts and it has helped me a little bit but I do not see how they can solve my problem.

Running R script_Readline and Scan does not pause for user input

stopping the script until a value is entred from keyboard in R

R Language - Waiting for user input with scan or readline

0条回答
登录 后发表回答