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]]
stop('Different number of assets! \n\n')
X
Bill Cunliffe ??:> 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.
>
>
Bill Cunliffe 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've wondered about that myself Have you tried return() ? It would return NULL I think, as long as that doesn't cause a problem it should exit the method.
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.
>
You might try to use "on.exit" or "stop"?
# on.exit
if (nAssetPositions != nAssetPrices) {
on.exit(cat("Different number of assets! "\n"))
}
# stop
if (nAssetPositions != nAssetPrices) {
stop("Different number of assets!")
}
You could find these in "S programming" by W.N Venables and B.D.
Ripley
Chunhao
Quoting Bill Cunliffe <bill at elevationllc.net>:
> 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.
>
Try using stop:
if (nAssetPositions != nAssetPrices) {
stop("Different number of assets! \n\n")
On Thu, May 29, 2008 at 3:23 PM, Bill Cunliffe <bill@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@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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]
Hi Bill Cunliffe 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 think the easiest thing to use is the 'stopifnot()' function. afunction <- function(myinput) { stopifnot(is.numeric(myinput)) return(myinput * 2) } Alternatively, you can also do: afunction2 <- function(myinput) { if (!is.numeric(myinput)) stop("Input not numeric") return(myinput * 2) } > afunction("roland") Error: is.numeric(myinput) is not TRUE > afunction2("roland") Error in afunction2("roland") : Input not numeric > afunction2(123) [1] 246 > afunction(123) [1] 246 > I hope this helps, Roland
See ?return HTH, Chuck On Thu, 29 May 2008, Bill Cunliffe 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901