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

Henrik Bengtsson hb at biostat.ucsf.edu
Sun Nov 21 17:17:10 CET 2010


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



More information about the R-help mailing list