7

I am have a piece of code that prompts for user input and works perfectly fine in while running the code in Windows. However, in Linux, it executes every line without waiting for user input.

I have added the code in a separate function and used system("stty -echo") without success. Why is this happening and what can be done ? (execution 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
}
3
  • How are you running Rscript test.R in Windows? Are you running it from the Windows command line? I ask because readline shouldn't prompt for user input when called from a non-interactive session, regardless of the platform. Commented Mar 10, 2012 at 19:05
  • @Joshua Ulrich - I am running it in Linux using that command. Commented Mar 10, 2012 at 19:11
  • @JoshuaUlrich- However, I would like to have an interactive session, on Linux Commented Mar 10, 2012 at 19:16

1 Answer 1

7

You might want to consider using readLines from "stdin". The prompt function is designed for documenting 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
Sign up to request clarification or add additional context in comments.

1 Comment

-I have tried it and it works as it is..I was trying to adapt it to my code to see how it works..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.