Wait for user input when running R script on Linux

I have a piece of code that asks for user input and works great when running code on Windows. However, on Linux, it runs every line without waiting for user input.

I added the code to a separate function and used the system ("stty -echo") without success. Why is this happening and what can be done? (run command: rscript test.R)

require(Biostrings) read_value <- function(prompt_text = "", prompt_suffix = getOption("prompt"), coerce_to= "character") { prompt <- paste(prompt_text, prompt_suffix) system("stty -echo") as(readline(prompt), coerce_to) } prints<-function() { opt<-as.character(readline(prompt = "Enter parameter values: ")) system("stty -echo") i<-1 while ((i<=5)) { if (i==1) { expr.filename <- as.character(readline(prompt = "Expression file name: ")) tryCatch( {expr.file<-read.table(expr.filename)},error=function(e) {print("ERROR : Enter valid filename!") return }) } if (i==2) { system("stty -echo") fasta.filename <- as.character(readline(prompt = "Fasta file name: ")) tryCatch( {sequence_data<-read.DNAStringSet(fasta.filename)},error=function(e) {print("ERROR : Enter valid filename!") return }) } #missing piece of code i<-i+1 } 
+7
source share
1 answer

You might want to use readLines from "stdin". The quick start function is designed to document datasets.

  cat("Please enter a file name ...") fil <- readLines(con="stdin", 1) cat(fil, "\n") 

Save as filnam.r .... Use:

 user-linux-prompt$ Rscript filnam.r 
+6
source

All Articles