Programming with R – Checking Function Arguments

October 24, 2010
By

(This article was first published on Software for Exploratory Data Analysis and Statistical Modelling, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , , , , ,

Comments are closed.

Sponsors

Mango solutions



plotly webpage

dominolab webpage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

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)