[R] Setting up infile for R CMD BATCH
ilai
keren at math.montana.edu
Wed Feb 8 20:08:39 CET 2012
Gang,
Maybe someone here has a different take on things. I'm afraid I have
no more insights on this unless you explain exactly what you are
trying to achieve, or more importantly why? That may help understand
what the problem really is.
Do you want to save an interactive session for future runs? then
?save.image and all your "answers" are in the environment. In this
case consider putting an "if(!exists('type') | length(type)<1 |
is.na(type))" before "type<- readline(...)" in your script so type
wouldn't be overwritten in subsequent runs.
If your goal is to batch evaluate multiple answer files from users
(why else would you ask questions with readline?), then you should
have enough to go on with my answer and the examples in ?eval.
Elai
On Wed, Feb 8, 2012 at 9:04 AM, Gang Chen <gangchen6 at gmail.com> wrote:
> Hi Elai,
>
> Thanks a lot for the suggestions! I really appreciate it...
>
> Your suggestion of using eval() and creating those answers in a list
> would work, but there is no alternative to readline() with which I
> could read the input in batch mode? I'm asking this because I'd like
> to have the program work in both interactive and batch mode.
>
> Thanks again,
> Gang
>
>
> On Wed, Feb 8, 2012 at 12:50 AM, 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
More information about the R-help
mailing list