Skip to Content
Modern R Programming Cookbook
book

Modern R Programming Cookbook

by Jaynal Abedin
October 2017
Beginner to intermediate
236 pages
7h 38m
English
Packt Publishing
Content preview from Modern R Programming Cookbook

How to do it…

Let’s write the function with the error handler implemented inside the body of the function. There are several ways to handle the exception, using the following steps:

  1. Let's take a look at the use of the stop() function in the following code snippet:
        logX <- function(numVec1){          if(any(numVec1<0))            stop("A negative value exists")          else logV1 <- log(numVec1)          return(logV1)        }
  1. Let's take a look at the use of the try() function in the following code snippet:
        logX1 <- function(numVec1){          try(logV1 <- log(numVec1))          return(logV1)        }
  1. Let's take a look at the use of the tryCatch() function in the following code snippet:
        logX2 <- function(numVec1){          tryCatch(            return(logV1 <- log(numVec1)), warning = function(w) print("A negative input ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R Cookbook, 2nd Edition

R Cookbook, 2nd Edition

JD Long, Paul Teetor

Publisher Resources

ISBN: 9781787129054Supplemental Content