Wait for user input from keyboard in R before next line of code - readline - Rstudio

旧时模样 提交于 2019-11-28 10:21:43

问题


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

来源:https://stackoverflow.com/questions/39245442/wait-for-user-input-from-keyboard-in-r-before-next-line-of-code-readline-rst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!