tags:

views:

126

answers:

2

Simple (?) question:

How can I make a R script to stop? So no normal ending of all the code. Is there something like a 'return' or 'exit' function? I want ending my script to run in case of an error. Cannot find it in the documentation?

+2  A: 

q()

help (q)

quit package:base
R Documentation

Terminate an R Session

Description:

 The function 'quit' or its alias 'q' terminate the current R
 session.

Usage:

 quit(save = "default", status = 0, runLast = TRUE)
    q(save = "default", status = 0, runLast = TRUE)
 .Last <- function(x) { ...... }

Arguments:

save: a character string indicating whether the environment
      (workspace) should be saved, one of '"no"', '"yes"', '"ask"'
      or '"default"'.

status: the (numerical) error status to be returned to the operating system, where relevant. Conventionally '0' indicates successful completion.

runLast: should '.Last()' be executed?

Details:

 'save' must be one of '"no"', '"yes"', '"ask"' or '"default"'.  In
 the first case the workspace is not saved, in the second it is
 saved and in the third the user is prompted and can also decide
 _not_ to quit.  The default is to ask in interactive use but may
 be overridden by command-line arguments (which must be supplied in
 non-interactive use).

 Immediately _before_ terminating, the function '.Last()' is
 executed if it exists and 'runLast' is true. If in interactive

use there are errors in the '.Last' function, control will be returned to the command prompt, so do test the function thoroughly. There is a system analogue, '.Last.sys()', which is run after '.Last()' if 'runLast' is true.

 Some error statuses are used by R itself.  The default error
 handler for non-interactive use effectively calls 'q("no", 1,
 FALSE)' and returns error code 1.  Error status 2 is used for R
 ‘suicide’, that is a catastrophic failure, and other small numbers
 are used by specific ports for initialization failures.  It is
 recommended that users choose statuses of 10 or more.

 Valid values of 'status' are system-dependent, but '0:255' are
 normally valid.  (Many OSes will report the last byte of the
 value, that is report the number modulo 256.  But not all.)

 Windows calls the status the ‘error code’ or ‘exit code’.  It is
 returned in the environment variable '%ERRORLEVEL%' in 'cmd.exe',
 and in 'LASTEXITCODE' in Windows PowerShell.  Note that the
 'Rterm' reliably reports the 'status' value, but 'Rgui' may give
 an error code from the GUI interface.

References:

 Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
 Language_. Wadsworth & Brooks/Cole.

See Also:

 '.First' for setting things on startup.

Examples:

 ## Not run: 
 ## Unix-flavour example
 .Last <- function() {
   cat("Now sending PostScript graphics to the printer:\n")
   system("lpr Rplots.ps")
   cat("bye bye...\n")
 }
 quit("yes")
 ## End(Not run)
bua
+2  A: 

Were you able to find stop in the documentation? If so, what behavior do you want that stop does not provide?

Joshua Ulrich
`quit()` is ok for me
waanders
How were you unable to find `quit` in the documentation? It's used in the first section of the manual, [An Introduction to R](http://cran.r-project.org/doc/manuals/R-intro.html#Using-R-interactively)...
Joshua Ulrich
My point is that you said you could not find it in the documentation but the answer you accepted is on the 3rd page of the introductory manual, so it seems like you did not look at the basic documentation. Reading the _introduction_ to the introductory manual would be the first place to look, not part of an "extensive" search.
Joshua Ulrich
Sorry for being a bit lazy
waanders