How to Include Interactive Input in Script to Be Run from the Command Line

How to include interactive input in script to be run from the command line

Try this:

cat("What's your name? ")
x <- readLines(file("stdin"),1)
print(x)

Hopefully some variant of that works for you.

How to include interactive input in script to be run from the command line

Try this:

cat("What's your name? ")
x <- readLines(file("stdin"),1)
print(x)

Hopefully some variant of that works for you.

Passing arguments to an interactive program non-interactively

For more complex tasks there is expect ( http://en.wikipedia.org/wiki/Expect ).
It basically simulates a user, you can code a script how to react to specific program outputs and related stuff.

This also works in cases like ssh that prohibits piping passwords to it.

R script with user input from command line

Here is a total hack, repurposing a very specific purpose-built package for your more-general question:

library(getPass)
userInput<-function(question) {
n = 0
while(n < 1 ){
n <- getPass::getPass(msg = question)
n <- ifelse(grepl("\\D",n),-1,as.integer(n))
if(is.na(n)){break} # breaks when hit enter
}
return(n)
}

investedLow <- userInput("Invested value in low risk since last time: ")
print(investedLow)

Maybe the worst part about this is that getPass hides the user input. There must be a way to modify the source code to fix that.

Update: The getPass author pointed out that the solution could be as simple as using readLines slightly differently:

cat(question)
readLines(file("stdin"), n=1)

bash interactive script pass input

If you want to redirect the normal standard input of the program, you could use so called "here documents" (see e.g. the BASH manual page):

java -jar script.jar <<EOF
your input here
EOF

That means standard input (a.k.a. stdin) is redirected and will be the text in the "here document", in this case your input here.

Interactive bash script receiving user inputs

Here is the skeleton of an answer to help you ask a better question:

sed -n '/value/s/\([^:]*\):.*/\1/p' file | while read d
do
(
cd d
ls | grep .fileformat
read -p "3 commands? " c
[ -z "$c" ] && continue
cd d
eval "$c"
)
done

(Usual caveats of reading interactively + using eval being dangerous)

How to get a user input in command prompt and pass it to R

From the documentation of readline():

This can only be used in an interactive session. [...] In non-interactive use the result is as if the response was RETURN and the value is "".

For non-interactive use - when calling R from the command line - I think you've got two options:

  1. Use readLines(con = "stdin", n = 1) to read user input from the terminal.
  2. Use commandArgs(trailingOnly = TRUE) to supply the input as an argument from the command line when calling the script instead.

Under is more information.

1. Using readLines()

readLines() looks very similar to readline() which you're using, but is meant to read files line by line. If we instead of a file points it to the standard input (con = "stdin") it will read user input from the terminal. We set n = 1 so that it stops reading from the command line when you press Enter (that is, it only read one line).

Example

Use readLines() in a R-script:

# some-r-file.R

# This is our prompt, since readLines doesn't provide one
cat("Please write something: ")
args <- readLines(con = "stdin", n = 1)

writeLines(args[[1]], "output.txt")

Call the script:

Rscript.exe "some-r-file.R"

It will now ask you for your input. Here is a screen capture from PowerShell, where I supplied "Any text!".

Print screen from PowerShell

Then the output.txt will contain:

Any text!


2. UsingcommandArgs()

When calling an Rscript.exe from the terminal, you can add extra arguments. With commandArgs() you can capture these arguments and use them in your code.

Example:

Use commandArgs() in a R-script:

# some-r-file.R
args <- commandArgs(trailingOnly = TRUE)

writeLines(args[[1]], "output.txt")

Call the script:

Rscript.exe "some-r-file.R" "Any text!"

Then the output.txt will contain:

Any text!


Related Topics



Leave a reply



Submit