Marc Aurel Kiefer
2013-Feb-26 10:07 UTC
[Rd] Running R scripts with interactive-style evaluation
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. 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. It doesn't matter if it's a source()-like way or "R CMD BATCH" or even feeding stdin to R or whatever... Regards, Marc
Duncan Murdoch
2013-Feb-26 13:06 UTC
[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
William Dunlap
2013-Feb-26 16:47 UTC
[Rd] Running R scripts with interactive-style evaluation
Which part of the read-eval-print loop loop ("REPL loop") do you need? source(file, print=TRUE) gives you the printing part, which is what I usually want. Opening a file connection and repeatedly calling parse(n=1) gives you the read part, > tf <- tempfile() > cat(file=tf, sep="\n", "x <- 1 +", "10 ; y <- 2:7", "10:3") > f <- file(tf, open="rt") > parse(f, n=1) expression(x <- 1 + 10) > parse(f, n=1) expression(y <- 2:7) > parse(f, n=1) expression(10:3) > parse(f, n=1) expression() > close(f) and you can copy the code from source() to get the eval and print stuff right. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf > Of Marc Aurel Kiefer > Sent: Tuesday, February 26, 2013 2:08 AM > To: r-devel at r-project.org > Subject: [Rd] Running R scripts with interactive-style evaluation > > 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. > > 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. > > It doesn't matter if it's a source()-like way or "R CMD BATCH" or even feeding stdin to R > or whatever... > > Regards, > > Marc > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Jon Clayden
2013-Feb-26 17:41 UTC
[Rd] Running R scripts with interactive-style evaluation
If you're intending to run some code that may require user input, then I share your need. I started two threads on this some time ago [1,2], but as far as I know it still isn't possible. My workaround is to use "expect", or to create a temporary .Rprofile if that is not available, from within a shell script wrapper (see [3], lines 197-221). It isn't pretty, and I'd love to see support for this kind of use case in R proper (happy to contribute my time to help if someone with better knowledge of the R source could guide me), but it's the best solution I've found. All the best, Jon -- [1] https://stat.ethz.ch/pipermail/r-help/2008-January/150786.html [2] https://stat.ethz.ch/pipermail/r-devel/2008-September/050803.html [3] https://github.com/jonclayden/tractor/blob/master/bin/tractor#L197 On 26 February 2013 10:07, Marc Aurel Kiefer <marcaurelkiefer@gmx.de> 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. > > 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. > > It doesn't matter if it's a source()-like way or "R CMD BATCH" or even > feeding stdin to R or whatever... > > Regards, > > Marc > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]