2

I want to run a program in batch and I want it to wait for the user input in the program.

This is what I have:

library('XLConnect')
FolderPath<-"C:/Documents/Testing/"
load(paste('C:/Documents/Testing/Program.ml',sep=''));Run()

This code is named Test.R, and it calls a compiled file that runs a function that does what I need. I need to have some inputs in this exercise, so I wanted to ask for the user to input some dates.

The program should do something like this:

What are the years for this simulation?

Then the user enter:

>2001, 2002, 2003, 2004

The program will save this vector in variable, lets call y.
Then, when I load the compiled function, I will use y.
The problem is that I run this R code in cmd batch.

dekio
  • 810
  • 3
  • 16
  • 33

3 Answers3

1

Yukio, try the function readline (documentation here) as in the following example, in which a user inputs two years separated by a comma.

> years = readline('What are the years for this simulation? ')
What are the years for this simulation? 2001, 2002
> years = as.numeric(unlist(strsplit(years, ",")))
> years
[1] 2001 2002

By the way, your English is great!

Gyan Veda
  • 6,309
  • 11
  • 41
  • 66
  • I've tried, unfortunately readline() doesn't work when running in batch. I'm really struggling to get this part of my program done. tks for the compliment! – dekio Nov 06 '13 at 17:29
  • For the record, as of 12/2021, this still does not work – obewanjacobi Dec 28 '21 at 18:41
1

As far as I understand (someone please correct me if this is wrong), but you can't send messages to the user at the command line when executing a script with R CMD BATCH (it writes all input and output to an .Rout file). But executing a script with Rscript.exe does in fact let you send stuff to stdout. However, in order to get input from a user when executing with Rscript.exe, you need to use file('stdin') as the connection to your input function, which, funny enough, won't work if you then try to run the script from the R interpreter with the source function. Here's an example that showing you how to prompt the user for a series of comma separated years, regardless of whether the script runs with Rscript.exe or the source function in interactive mode.

con <- if (interactive()) stdin() else file('stdin')
message('what are the years')
years <- scan(file=con, sep=',', nlines=1, quiet=TRUE)
print(years)
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
1

You can deal easily with user inputs in a batch file not in the R. You create a batch file , say launcher.bat , like this for example

ECHO I am Rscript launcher
set /p years= What are the years for this simulation?
cd R_SCRIPT_PATH
Rscript youscript.R %years%

The user can write as many years he want, it will go in the variable years. Then in your script you parse this variable to set it as a valid list of years.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • I'm feeling really stupid here. I didn't understand very well. How should I call this .bat in my R code? – dekio Nov 06 '13 at 17:33
  • :) I am using Rscript not R CMD BATCH to launch the script. Maybe see [may answer here](http://stackoverflow.com/questions/13721872/making-commandargs-comma-delimited-or-parsing-spaces/13722382#13722382) to understand better. – agstudy Nov 06 '13 at 17:36
  • Hmmm... I get it, but this might be a problem as the whole program is running with CMD BATCH. – dekio Nov 06 '13 at 17:39