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 r r

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.


What worked for me on Windows with RStudio 0.98.945 and R version 3.1.1 was:

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


The answer by @Joshua Ulrich is fine for Linux, but hangs under macOS and needs to be terminated using Ctrl-D.

This is a work-around for both Linux and macOS:

#!/usr/bin/env Rscriptprint(system("read -p 'Prompt: ' input; echo $input", intern = TRUE))