How to validate input to the function?

Validating input to a function in R is crucial for ensuring that your code behaves as expected and can handle various types of input gracefully. R language offers various methods to validate input to the function. By using these methods provided by R, it is possible to check how the input is valid to the function. Some of the methods are:

Simple Input Validation

This example demonstrates a function that validates if the input is numeric. If the input is not numeric, it throws an error indicating that the input must be numeric.

R
validate_input <- function(x) {
  if (!is.numeric(x)) {
    stop("Input must be numeric")
  }
  return(x)
}

# Test the function
validate_input("a")

Output:

Error in validate_input("a") : Input must be numeric

In the above, the output is an error. So, to handle these error we need input must be a numeric. After assigning the input numeric (123), the output is:

R
validate_input <- function(x) {
  if (!is.numeric(x)) {
    stop("Input must be numeric")
  }
  return(x)
}

# Test the function
validate_input(123)

Output:

 [1] 123

Type and Length Validation

Here, a function is provided to validate if the input is a character string with a minimum length of three characters. If the input does not meet this criteria, it throws an error specifying the requirement for a character string with a minimum length.

R
validate_input_type_length <- function(x) {
  if (!is.character(x) || nchar(x) < 3) {
    stop("Input must be a character string with at least 3 characters")
  }
  return(x)
}

# Test the function
validate_input_type_length(123)   # Invalid input

Output:

Error in validate_input_type_length(123) : Input must be a character string with at least 3 characters

In the above, the output is an error. So, to handle these error we need assign the input must be a character string with at least 3 characters. After assigning the valid input (abc), the output is:

R
validate_input_type_length <- function(x) {
  if (!is.character(x) || nchar(x) < 3) {
    stop("Input must be a character string with at least 3 characters")
  }
  return(x)
}

# Test the function
validate_input_type_length('abc')   

Output:

[1] "abc"

Custom Validation

This example showcases a function that applies custom validation to the input. In this case, it checks if all elements in the input vector are non-negative. If any element is negative, it throws an error indicating that the input must be non-negative.

R
custom_validation <- function(x) {
  if (any(x < 0)) {
    stop("Input must be non-negative")
  }
  return(x)
}

# Test the function
custom_validation(c(-1, 2, 3))  

Output:

Error in custom_validation(c(-1, 2, 3)) : Input must be non-negative

In the above, the output is an error. So, to handle these error we need assign the input must be non-negative. After assigning the valid input c(1,2,3), the output is:

R
custom_validation <- function(x) {
  if (any(x < 0)) {
    stop("Input must be non-negative")
  }
  return(x)
}

# Test the function
custom_validation(c(1, 2, 3))  

Output:

[1] 1 2 3

Checking for Missing Values

This example demonstrates a function that checks if the input contains any missing values. If any missing values are found, it throws an error indicating that the input contains missing values.

R
validate_input_missing <- function(x) {
  if (any(is.na(x))) {
    stop("Input contains missing values")
  }
  return(x)
}

# Test the function
validate_input_missing(c(1, 2, NA)) # Invalid input

Output:

Error in validate_input_missing(c(1, 2, NA)) : Input contains missing values

In the above, the output is an error. So, to handle these error we need assign the input must does not contains missing values. After assigning the valid input c(40, 50, 60), the output is:

R
validate_input_missing <- function(x) {
  if (any(is.na(x))) {
    stop("Input contains missing values")
  }
  return(x)
}

# Test the function
validate_input_missing(c(40, 50, 60)) 

Output:

[1] 40 50 60

Vector Length Validation

Here, a function is provided to validate if the length of the input vector matches a specified required length. If the length of the input vector does not match the required length, it throws an error specifying the required length for the input vector.

R
validate_input_vector_length <- function(x, required_length) {
  if (length(x) != required_length) {
    stop(paste("Input vector length must be", required_length))
  }
  return(x)
}

# Test the function
validate_input_vector_length(c(1, 2), 3) # Invalid input

Output:

Error in validate_input_vector_length(c(1, 2), 3) : Input vector length must be 3

In the above, the output is an error. So, to handle these error we need assign the input of vector length must be 3. After assigning the valid input (c(1, 2, 3), 3) , the output is:

R
validate_input_vector_length <- function(x, required_length) {
  if (length(x) != required_length) {
    stop(paste("Input vector length must be", required_length))
  }
  return(x)
}

# Test the function
validate_input_vector_length(c(1, 2,3), 3)

Output:

[1] 1 2 3

Logical Input Validation

This example illustrates a function that validates if the input is of logical type. If the input is not logical, it throws an error indicating that the input must be logical.

R
validate_input_logical <- function(x) {
  if (!is.logical(x)) {
    stop("Input must be logical")
  }
  return(x)
}

# Test the function
validate_input_logical(1)     # Invalid input

Output:

Error in validate_input_logical(1) : Input must be logical

In the above, the output is an error. So, to handle these error we need assign the input must be logical. After assigning the valid input (TRUE), the output is:

R
validate_input_logical <- function(x) {
  if (!is.logical(x)) {
    stop("Input must be logical")
  }
  return(x)
}

# Test the function
validate_input_logical(TRUE) 

Output:

[1] TRUE

How to Validate Input to a Function Error in R

In this article, we will examine various methods for how to validate input to the function by using R Programming Language.

Similar Reads

How to validate input to the function?

Validating input to a function in R is crucial for ensuring that your code behaves as expected and can handle various types of input gracefully. R language offers various methods to validate input to the function. By using these methods provided by R, it is possible to check how the input is valid to the function. Some of the methods are:...

Conclusion

In conclusion, we learned about various ways of how to validate input to the function in R. R language offers versatile tools and functions while dealing with validation of input....

Contact Us