[R] How do you exit a function in R?

Douglas Bates bates at stat.wisc.edu
Thu May 29 20:38:25 CEST 2008


It looks like you want to stop the function execution on detecting an
error condition, in which case the appropriate function to call is
stop(), as in

if (nAssetPositions != nAssetPrices)
   stop("Different number of assets!")

An alternative, if you don't want to write the error messages for each
condition, is to use stopifnot(), as in

stopifnot(nAssetPositions == nAssetPrices)


On Thu, May 29, 2008 at 1:23 PM, Bill Cunliffe <bill at elevationllc.net> wrote:
> For example, based on a certain condition, I may want to exit my code early:
>
>
>
>            # Are there the same number of assets in "prices" and
> "positions"?
>
>            if (nAssetPositions != nAssetPrices) {
>
>                        cat("Different number of assets! \n\n")
>
>                        <exit function>
>
>            }
>
>
>
> I have searched, but not found, a way of forcing a function to exit.  Any
> help greatly appreciated.
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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