From jon.clayden at gmail.com  Thu Sep 25 17:37:53 2008
From: jon.clayden at gmail.com (Jon Clayden)
Date: Thu, 25 Sep 2008 16:37:53 +0100
Subject: [Rd] R --interactive and readline() creates infinite loop
In-Reply-To: <48DBA1A8.2030602@r-project.org>
References: <98A53690-6407-4E86-A094-27707C77C744@ucl.ac.uk>
	<48DB8E97.9000609@r-project.org> <48DB93F7.8000509@biostat.ku.dk>
	<48DBA1A8.2030602@r-project.org>
Message-ID: <68e086180809250837h282f9226w4ae0875a7a0d34e4@mail.gmail.com>

Many thanks for the suggestions.

The use case I had in mind is more along the lines of what Peter
Dalgaard mentioned, and his solution looks ideal. I know in advance
exactly what code I want to run, but some of that code may require
user input.

Jon

2008/9/25 John Chambers <jmc at r-project.org>:
> My application, at least, wanted to show (my class) individual commands
> from the file and then optionally insert some typed commands before
> going on to the next part of the source file. As far as I can see, the
> piped shell command approach will have to treat the whole file at one time.
>
> John
>
> Peter Dalgaard wrote:
>> One canonical way of doing it is:
>>
>> (echo 'print(readline("Input:"))'; cat -) | R --interactive --no-save
>>
>> (you don't want to leave out --no-save ....)
>>
>>
>> John Chambers wrote:
>>
>>> For an alternative approach to your original goal, take a look at
>>> demoSource() in the SoDA package from CRAN.  It's a bit tedious to set
>>> up (see the Details section of the help file) but uses standard R
>>> sessions to mix lines from a demo file and interactive input.
>>>
>>> John
>>>
>>> Jon Clayden wrote:
>>>
>>>> Dear all,
>>>>
>>>> I have asked before, in R-help [1], about a way to create an
>>>> interactive session in which commands are taken from a file or
>>>> standard input - like R CMD BATCH but additionally allowing user
>>>> input - but there was no response to that question, and the
>>>> workarounds that I have found (using "expect", creating a temporary
>>>> .Rprofile) are ugly and problematic.
>>>>
>>>> With the appearance of the --interactive flag in R 2.7.0 I thought
>>>> this might become possible, but it not only does not behave as I
>>>> would expect, it appears to go into an infinite loop, and uses 100%
>>>> CPU until killed.
>>>>
>>>> $ echo 'print(readline("Input:"))' | R --no-save --quiet
>>>>
>>>>> print(readline("Input:"))
>>>>>
>>>> Input:[1] ""
>>>>
>>>> [no interactivity]
>>>>
>>>> $ echo 'print(readline("Input:"))' | R --no-save --quiet --interactive
>>>> [no response at all]
>>>>
>>>> This behaviour remains in the latest alphas of R 2.8.0. My platform
>>>> is Mac OS X.5.5 on Intel Core 2 Duo.
>>>>
>>>> I assume, given this outcome, that this is not the intended use of
>>>> --interactive, but I still wonder if there is any way to achieve an
>>>> interactive session based on a predefined set of commands without
>>>> writing a completely new front-end (overkill, surely?).
>>>>
>>>> Any guidance would be appreciated.
>>>>
>>>> Regards,
>>>> Jon
>>>>
>>>> [1] http://finzi.psych.upenn.edu/R/Rhelp02a/archive/117412.html
>>>>
>>>>
>>>> --
>>>> Jonathan D. Clayden, Ph.D.
>>>> Research Fellow
>>>> Radiology and Physics Unit
>>>> UCL Institute of Child Health
>>>> 30 Guilford Street
>>>> LONDON  WC1N 1EH
>>>> United Kingdom
>>>>
>>>> t | +44 (0)20 7905 2708
>>>> f | +44 (0)20 7905 2358
>>>> w | www.homepages.ucl.ac.uk/~sejjjd2/
>>>>
>>>> ______________________________________________
>>>> R-devel at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>
>>
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] R --interactive and readline() creates infinite loop
Jon Clayden