100%(3)3 out of 3 people found this document helpful
This preview shows page 127 - 130 out of 636 pages.
> doNoWork <- function(filename) {+ if(file.exists(filename)) {+ "la la la"+ } else {+ warning("File does not exist: ", filename)+ }+ }> doNoWork("another file that doesn't exist")Warning message:In doNoWork("another file that doesn't exist") :File does not exist: another file that doesn't existIf you just want to tell the user something, you can use the messagefunction:> doNothing("another input value")This function does nothing.Catching ErrorsSuppose that you are writing a function in R called foothat calls another functioncalled bar. Furthermore, suppose that barsometimes generates an error, but youdon’t want footo stop if the error is generated. For example, maybe bartries to opena file, but signals an error when it can’t open the file. If barcan’t open the file, maybeyou want footo try doing something else instead.A simple way to do this is to use the tryfunction. This function hides some of thecomplexity of R’s exception handling. Here’s an example of how to use try:> res <- try({x <- 1}, silent=TRUE)> res[1] 1> res <- try({open("file that doesn't exist")}, silent=TRUE)> res[1] "Error in UseMethod(\"open\") : \n no applicable method for 'open'applied to an object of class \"character\"\n"attr(,"class")[1] "try-error"Exceptions|105Environments andSymbols
The tryfunction takes two arguments, exprand silent. The first argument, expr,is the R expression to be tried (often a function call). The second argument specifieswhether the error message should be printed to the R console (or stderr); the defaultis to print errors. If the expression results in an error, then tryreturns an object ofclass "try-error".A more capable function is tryCatch. The tryCatchfunction takes three sets of ar-guments: an expression to try, a set of handlers for different conditions, and a finalexpression to evaluate. For example, suppose that the following call was made totryCatch:> tryCatch(expression, handler1, handler2, ..., finally=finalexpr)The R interpreter would first evaluate expression. If a condition occurs (an error orwarning), R will pick the appropriate handler for the condition (matching the classof the condition to the arguments for the handler). After the expression has beenevaluated, finalexprwill be evaluated. (The handlers will not be active when thisexpression is evaluated.)106|Chapter 8:Symbols and Environments
9FunctionsFunctions are the R objects that evaluate a set of input arguments and return anoutput value. This chapter explains how to create and use functions in R.The Function KeywordIn R, function objects are defined with this syntax:function(arguments) bodywhere argumentsis a set of symbol names (and, optionally, default values) that willbe defined within the body of the function, and bodyis an R expression. Typically,the body is enclosed in curly braces, but it does not have to be if the body is a singleexpression. For example, the following two definitions are equivalent:> f <- function(x,y) x+y> f <- function(x,y) {x+y}ArgumentsA function definition in R includes the names of arguments. Optionally, it may in-clude default values. If you specify a default value for an argument, then the argu-