[R] Setting up infile for R CMD BATCH

ilai keren at math.montana.edu
Wed Feb 8 06:51:52 CET 2012


On Tue, Feb 7, 2012 at 10:50 PM, ilai <keren at math.montana.edu> wrote:
> Ahh,
> I think I'm getting it now. Well, readlines() is not going to work for
> you. The help file ?readline clearly states "In non-interactive use
> the result is as if the response was RETURN and the value is ‘""’."
> The implication is you cannot use it to "insert" different answers as
> if you were really there.
> How about using eval() instead? You will need to make the answers a
> named list (or just assigned objects).
>
> test <- expression({
>  if(a>2) print('+')
>  else print('I got more')
>  b <- b+3   # reassign b in the environment
>  print(b)
>  print(c)
>  d^2
> })
> dump('test',file='myTest.R') ; rm(test)
>
> # make the answers.R file:
>
> a=5 ; b=2 ; c=2 ; d=3
> source("myTest.R")
> eval(test)
>
> # Now, from the terminal  R CMD BATCH answers.R out.R
> # And here is my $ cat out.R
> ... flushed
>> a=5 ; b=2 ; c=2 ; d=3
>> source("myTest.R")
>> eval(test)
> [1] "+"
> [1] 5
> [1] 2
> [1] 9
>>
>> proc.time()
>   user  system elapsed
>  0.640   0.048   0.720
>
> Would this work?
> Elai
>
>
>
>
> On Tue, Feb 7, 2012 at 4:05 PM, Gang Chen <gangchen6 at gmail.com> wrote:
>> Suppose I create an R program called myTest.R with only one line like
>> the following:
>>
>> type <- as.integer(readline("input type (1: type1; 2: type2)? "))
>>
>> Then I'd like to run myTest.R in batch mode by constructing an input
>> file called answers.R with the following:
>>
>> source("myTest.R")
>> 1
>>
>> When I ran the following at the terminal:
>>
>> R CMD BATCH answer.R output.Rout
>>
>> it failed to pick up the answer '1' from the 2nd line in answers.R as
>> shown inside output.Rout:
>>
>>> source("myTest.R")
>> input type (0: quit; 1: type1; 2: type2)?
>>> 1
>> [1] 1
>>
>> What am I missing here?
>>
>> Thanks in advance,
>> Gang
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list