Do R wait for console entry?

Is there a way to make R wait for console input before continuing? Let's say I use 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 an instruction readLinethat asks the user for a username. Unfortunately, if I just run the entire run.R file, R does not wait for the username to be entered. It runs script2.R before entering the username, which results in an error because the second script requires a username.

I have an ugly workaround for this, using R Studio .rs.askForPassword, which actually waits for input but closes the password. Which is great for passwords, but not so much for usernames. It is also a function of RStudio, not R.

+4
3

scan .

test.R :

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)

:

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

- edit-- , :

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

what , nmax quiet , , , .

+4

readline . readline . ?interactive :

GUI R . R ( Rterm.exe Windows), , , 'stdin () , 'Stdin . --interactive (Unix) -ess (Windows, Rterm.exe) .

+3

Why not put

source("script2.R")

to script1.R file?

0
source

Source: https://habr.com/ru/post/1535877/


All Articles