Programming with R – Checking Function Arguments

[This article was first published on Software for Exploratory Data Analysis and Statistical Modelling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In a previous post we considered writing a simple function to calculate the volume of a cylinder by specifying the height and radius of the cylinder. The function did not have any checking of the validity of the function arguments which we will consider in this post.

R has various functions that we can use to test certain conditions in our function. These include the functions stop, warning and conditional statements such as if statements combined with stop or warning.

As an example consider extending the function to calculate volumes to test whether either the height or radius has not been submitted when the function is called. We will make use of the missing function that tests whether a specific argument has been provided with the function call. The new function is:

cylinder.volume.2 = function(height, radius)
{
    if (missing(height))
        stop("Need to specify height of cylinder for calculations.")
 
    if (missing(radius))
        stop("Need to specify radius of cylinder for calculations.")
 
    volume = pi * radius * radius * height
 
    volume
}

We use the if statement to test whether each of the arguments is missing and when they are the function is stopped and an error message is written to the console. Here are a couple of examples of the function being halted when insufficient information is provided:

> cylinder.volume.2(height = 7)
Error in cylinder.volume.2(height = 7) : 
  Need to specify radius of cylinder for calculations.
> 
> cylinder.volume.2(radius = 10)
Error in cylinder.volume.2(radius = 10) : 
  Need to specify height of cylinder for calculations.

So this handles one particular type of problem with the function but there are other checks that we might want to make. For example, negative values for the height or radius are not sensible and should also lead to an error. We can check this condition using an if statement in the function:

cylinder.volume.3 = function(height, radius)
{
    if (missing(height))
        stop("Need to specify height of cylinder for calculations.")
 
    if (missing(radius))
        stop("Need to specify radius of cylinder for calculations.")
 
    if (height < 0)
        stop("Negative height specified.")
 
    if (radius < 0)
        stop("Negative radius specified.")
 
    volume = pi * radius * radius * height
 
    volume
}

An example of the function in action:

> cylinder.volume.3(10, -4)
Error in cylinder.volume.3(10, -4) : Negative radius specified.

These are a couple of basic examples of validation that we can include in our function that will hopefully allow us to catch erratic behaviour in software which is more of an issue as programs get larger and more complicated.

To leave a comment for the author, please follow the link and comment on their blog: Software for Exploratory Data Analysis and Statistical Modelling.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)