[Rd] Running R scripts with interactive-style evaluation

William Dunlap wdunlap at tibco.com
Tue Feb 26 17:47:18 CET 2013


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



More information about the R-devel mailing list