[R] [beginner] simple keyword to exit script ?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 21 16:53:55 CET 2010


On Sun, Nov 21, 2010 at 9:52 AM, madr <madrazel at interia.pl> wrote:
>
> I have tried quit(), and return() but first exits from whole graphical
> interface and second is only for functions. What I need only to exit from
> current script and return to the console.

1. use my.source() below instead of source() to source your file and
2. in your file exit by using parent.frame(3)$exit.source(returncode).

Note that parent.frame(3)exit.source(returncode) must be at the top
level of your script.  If its buried within a function within the
script then you may have to increase 3 to some larger number to go
back the correct number of frames.   See ?callCC for more info.

Here is an example:

# use this in place of source
my.source <- function(file, ...) {
	source. <- function(exit.source) source(file, ...)
	callCC(source.)
}

# generate a test script called mytestfile.R
cat("cat('A\n')
parent.frame(3)$exit.source(7)  # exit script
cat('B\n')
", file = "mytestfile.R")

# run the test script
# It never gets to cat('B\n')
# exitcode contains the exit code, 7 in this case
exitcode <- my.source("mytestfile.R")

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list