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. -- View this message in context: http://r.789695.n4.nabble.com/beginner-simple-keyword-to-exit-script-tp3052417p3052417.html Sent from the R help mailing list archive at Nabble.com.
On Nov 21, 2010, at 9:52 AM, madr 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.?stop> --David Winsemius, MD West Hartford, CT
I try to use stop(), but i get: Error in eval.with.vis(expr, envir, enclos) : -- View this message in context: http://r.789695.n4.nabble.com/beginner-simple-keyword-to-exit-script-tp3052417p3052430.html Sent from the R help mailing list archive at Nabble.com.
stop() throws an error but the side effect is to pop out of whatever environment you may be in and return to the top-level. I thought that was what you wanted. If not, then please produce a better problem description with code as requested in the posting guide. On Nov 21, 2010, at 10:12 AM, madr wrote:> > I try to use stop(), but i get: > Error in eval.with.vis(expr, envir, enclos) : > -- > View this message in context: http://r.789695.n4.nabble.com/And learn to include context. -- David Winsemius, MD West Hartford, CT
Is there any way of suppressing that error, like in other programming languages you can specifically invoke an error or simply exit, like after user input, exiting then is not always identical with error , there are cases when it is intended behavior. I thought R makes that distinction. -- View this message in context: http://r.789695.n4.nabble.com/beginner-simple-keyword-to-exit-script-tp3052417p3052464.html Sent from the R help mailing list archive at Nabble.com.
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
On Nov 21, 2010, at 10:43 AM, madr wrote:> > Is there any way of suppressing that error, like in other programming > languages you can specifically invoke an error or simply exit,If you are in a function, then return()> like after > user input, exiting then is not always identical with error , there > are > cases when it is intended behavior. I thought R makes that > distinction.Provide some code. (You did say you had a script.) The answer probably depends on context and you are not providing any. -- David Winsemius, MD West Hartford, CT
Better is probably to return() from a function, i.e. define a function in your script and call that at the end of your function, e.g. - - - - main <- function() { # bla # bla if (something) { return(); } # otherwise # bla } main(); - - - - Otherwise, here is a hack: stopQuietly <- function(...) { blankMsg <- sprintf("\r%s\r", paste(rep(" ", getOption("width")-1L), collapse=" ")); stop(simpleError(blankMsg)); } # stopQuietly()> stopQuietly()>The "Error:" message will be replaced by blanks. It will still output an empty line. May not work in all settings; depends on terminal etc. You need to define stopQuietly() in your script. My $.02 /Henrik On Sun, Nov 21, 2010 at 7:56 AM, David Winsemius <dwinsemius at comcast.net> wrote:> > On Nov 21, 2010, at 10:43 AM, madr wrote: > >> >> Is there any way of suppressing that error, like in other programming >> languages you can specifically invoke an error or simply exit, > > If you are in a function, then return() > >> like after >> user input, exiting then is not always identical with error , there are >> cases when it is intended behavior. I thought R makes that distinction. > > Provide some code. (You did say you had a script.) ?The answer probably > depends on context and you are not providing any. > > -- > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Sun, Nov 21, 2010 at 10:56:14AM -0500, David Winsemius wrote:> On Nov 21, 2010, at 10:43 AM, madr wrote: > >Is there any way of suppressing that error, like in other programming > >languages you can specifically invoke an error or simply exit, > > If you are in a function, then return() > > >like after > >user input, exiting then is not always identical with error , there > >are > >cases when it is intended behavior. I thought R makes that > >distinction. > > Provide some code. (You did say you had a script.) The answer > probably depends on context and you are not providing any.A script may reach a condition, when it should stop in different situations. It need not be called using source(). It may be a function called from an extension package. If there is a sequence of several embedded calls than return() cannot be used. For scripts, which i use myself, i use stop() with a parameter, which is a character string explaining the stopping condition. This string is then a part of the generated error message. For scripts, which are used also by other people, i try to avoid this and make sure that after reaching a stopping condition, all cycles and function calls are finished. PS.