doNoWork functionfilename la la la else warningFile does

Donowork functionfilename la la la else warningfile

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 exist If you just want to tell the user something, you can use the message function: > doNothing("another input value") This function does nothing. Catching Errors Suppose that you are writing a function in R called foo that calls another function called bar . Furthermore, suppose that bar sometimes generates an error, but you don’t want foo to stop if the error is generated. For example, maybe bar tries to open a file, but signals an error when it can’t open the file. If bar can’t open the file, maybe you want foo to try doing something else instead. A simple way to do this is to use the try function. This function hides some of the complexity 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 | 105 Environments and Symbols
Image of page 127
The try function takes two arguments, expr and silent . The first argument, expr , is the R expression to be tried (often a function call). The second argument specifies whether the error message should be printed to the R console (or stderr); the default is to print errors. If the expression results in an error, then try returns an object of class "try-error" . A more capable function is tryCatch . The tryCatch function takes three sets of ar- guments: an expression to try, a set of handlers for different conditions, and a final expression to evaluate. For example, suppose that the following call was made to tryCatch : > tryCatch( expression , handler1 , handler2 , ..., finally= finalexpr ) The R interpreter would first evaluate expression . If a condition occurs (an error or warning), R will pick the appropriate handler for the condition (matching the class of the condition to the arguments for the handler). After the expression has been evaluated, finalexpr will be evaluated. (The handlers will not be active when this expression is evaluated.) 106 | Chapter 8: Symbols and Environments
Image of page 128
9 Functions Functions are the R objects that evaluate a set of input arguments and return an output value. This chapter explains how to create and use functions in R. The Function Keyword In R, function objects are defined with this syntax: function( arguments ) body where arguments is a set of symbol names (and, optionally, default values) that will be defined within the body of the function, and body is an R expression. Typically, the body is enclosed in curly braces, but it does not have to be if the body is a single expression. For example, the following two definitions are equivalent: > f <- function(x,y) x+y > f <- function(x,y) {x+y} Arguments A 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-
Image of page 129
Image of page 130

You've reached the end of your free preview.

Want to read all 636 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture