Skip to content
Back to formatted view

Raw Message

Message-ID: <CAK4FJ1AzwteRRDWQge2x8qMdk367w_RL7rE7uBOLoNU_AeXAYA@mail.gmail.com>
Date: 2012-02-08T05:51:52Z
From: ilai
Subject: Setting up infile for R CMD BATCH
In-Reply-To: <CAK4FJ1BzqVnzzxEz3e7Tbm9jfrbkvdX399U1GKLTK_S_nz4V8A@mail.gmail.com>

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.