[Rd] Running R scripts with interactive-style evaluation

Marc Aurel Kiefer marcaurelkiefer at gmx.de
Tue Feb 26 14:28:57 CET 2013

Thanks for your insights.

What I'm actually doing is the following:

I modified R in a way that the REPL loop always parses the input into an SEXPR, but depending on if "magic is enabled" or not, let R compute it or compute it via my own "backend".
As I wanted to keep the changes to R itself as minimally invasive as possible, there is a simple if-statement at the beginning of the REPL iteration which decides how to compute the expression.
using the R commands "enable/disable_magic()" one can change the behavior as required. This works fine when typing R code interactively, but not with a script, since it is only a single REPL iteration.
So I was looking for a way to get "interactive behavior" with scripts, without requiring too many changes to R itself.


Von: Duncan Murdoch [murdoch.duncan at gmail.com]
Gesendet: Dienstag, 26. Februar 2013 14:06
An: Marc Aurel Kiefer
Cc: r-devel at r-project.org
Betreff: Re: [Rd] Running R scripts with interactive-style evaluation

On 13-02-26 5:07 AM, Marc Aurel Kiefer wrote:
> Hi,
> when running a R-script like this:
> enable_magic()
> compute_stuff()
> disable_magic()
> the whole script is parsed into a single expression and then evaluated, whereas when using the interactive shell after each line entered, a REPL loop happens.

It's actually a vector of expressions that are evaluated one at a time,
but close enough...

> Is there a way to make a script evaluation behave like this, because I need a single REPL iteration for every expression in the script.

You don't say why you need that.  From your subject line, I'm guessing
you want to do something like a user prompt, then wait for user input,
then another user prompt, etc.? So the fact that disable_magic() was
parsed at the beginning shouldn't matter.  Or does it?

> It doesn't matter if it's a source()-like way or "R CMD BATCH" or even feeding stdin to R or whatever...

I think it all depends on what kind of input users are allowed to type,
but basically this is something you'll need to write yourself, I don't
think any of the normal running modes of R will suit you.  Take a look
at the source to source() or to the Sweave-related functions for ideas.

Duncan Murdoch

More information about the R-devel mailing list