4

Is there a way to make R wait for console input before continuing? Let's say I source two scripts like this in a main script called run.R:

# some more R Code here

source("script1.R")
source("script2.R")

# some more R Code there

Script1 contains some readLine statement that asks the user for a username. Unfortunately if I just run the entire run.R file R doesn't wait for the username to be entered. It starts script2.R before the username is entered which leads to an error because the 2nd script needs the username.

I have an ugly workaround for this using R Studio's .rs.askForPassword which actually waits for the the input, but covers the password. Which is cool for passwords, but not so much for usernames. Plus it's an RStudio feature, not R.

2
  • You can look at jverzani answer, it's doable with tcltk. Commented Apr 10, 2014 at 8:49
  • Hm, probably would not work on Rstudio server then? Or is tcltk als available from standard browser config? Commented Apr 10, 2014 at 8:55

3 Answers 3

4

readline can only be used in interactive sessions. In non-interactive use of readline the result is an empty string. On the help page of ?interactive you find the following about interactive sessions:

GUI consoles will arrange to start R in an interactive session. When R is run in a terminal (via Rterm.exe on Windows), it assumes that it is interactive if ‘stdin’ is connected to a (pseudo-)terminal and not if ‘stdin’ is redirected to a file or pipe. Command-line options --interactive (Unix) and --ess (Windows, Rterm.exe) override the default assumption.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, thx. I see now readline is not the way to go. Your answers does help to understand what I was guessing. But is there a way to circumvent this, i.e. use another function? I typically run R from RStudio.
4

Try scan function.

This is test.R file:

y <- 2
cat('y=',y)
cat("\nEnter username")
x <- scan(what=character(),nmax=1,quiet=TRUE)
"ala ma kota"
y <- 2*2
cat('y=',y)
cat('\nx=',x)

and then I run this:

> source("test.R")
y= 2
Enter username
1: login
y= 4
x= login

--edit-- To read only one character quietly run this:

> x <- scan(what=character(),nmax=1,quiet=TRUE)
1: username
> x
[1] "username"

what sets the type, nmax sets the maximal numbers of elements to read and quiet determine if print a line, saying how many items have been read.

4 Comments

Hmm, scan throws an error that it only accepts 'real' but usernames are characters often...
Is there an argument for to prompt something? Like "Enter username". Or would you just cat() it?
:( Actually this does not work either... script is executed before name is entered...
Works for me, check my post now. Maybe you've done something wrong?
0

Why not putting

source("script2.R")

into the file script1.R?

Comments

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.