If you wish to pause the execution of an R script until a user presses the Enter key, you can use the following pause function:
pause = function() { if (interactive()) { invisible(readline(prompt = "Press <Enter> to continue...")) } else { cat("Press <Enter> to continue...") invisible(readLines(file("stdin"), 1)) } }
This function works both in the interactive and batch processing modes, i.e., it works regardless of whether you are working within Rstudio, an R session in the terminal or if you are executing an R script using Rscript. Readers who are interested in learning more about the difference between these modes should take a look at this article.
Comments
I am able to stop the further evaluation of sourced code, or starting the next source() command if used in the top-level batch file. But I cannot continue afterwards, regardless of what I enter.
Any suggestions?
Using RStudio 0.99.896 with R version 3.2.5; x86_64-w64-mingw32/x64 (64-bit)
http://stackoverflow.com/questions/15272916/how-to-wait-for-a-keypress-in-r
pause()
message("works!")
Now run the script on a terminal with the command:
Rscript pause.R
What are the results which you observe? When you press <Enter>, is the message "works!" displayed afterwards?
pause()
f1<-readline(prompt = "enter the path of the biopsy file--")
pause()
f2<-readline(prompt = "enter the path of the treatment file--")
As you see if I run the above script without the pause() the command prompt overwrites the first readline and jumps to the next.
To fix the issue, try replacing your first readline statement with this:
cat("enter the location of the directory");
d <- readLines("stdin", 1);
Apply the same change to the subsequent readline statements. You can learn more about this topic here:
https://stackoverflow.com/questions/27112370/make-readline-wait-for-input-in-r
data.location = file.choose()
curr.data <- read.csv(data.location)