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

William Dunlap wdunlap at tibco.com
Sun Nov 21 19:49:25 CET 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Henrik Bengtsson
> Sent: Sunday, November 21, 2010 8:17 AM
> To: David Winsemius
> Cc: r-help; madr
> Subject: Re: [R] [beginner] simple keyword to exit script ?
> 
> 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.

This list gets a lot of questions about how to
do things with 'scripts' that are easily done with
functions.  The S language (implemented by R and S+)
is oriented around functions, not scripts.  Functions
easily call other functions but scripts cannot easily
call other scripts.  Scripts are handy for one-off
things, but if you want to use the code in them again
it is best to put it into functions (and, after not
too long, the functions into a package).  If you don't
accept that fact you have no end of frustration
with the language.  (I realize others may have
different opinions.) 

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> - - - -
> 
> 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.
> >
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list