Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validating input to a function in R

Tags:

r

I would like to error trap an input value to ensure that the user is entering the correct choice. In this case there are five choices "ns", "dl", "sv", "asv", "cs". I would like to check the use input against these if none of these are present then return and error msg, if blank default to "ns" and send a message to the user. I tried scanning over a vector string but that did not work. Any suggestions are appreciated

   method = "ns"
   if(method != scan(c("ns", "dl", "sv", "asv" ))) {"Invalid Value"} else {method = method}  
like image 357
no name Avatar asked Oct 14 '13 15:10

no name


People also ask

How do you validate a function?

The validate function takes any number of (unnamed) arguments, each of which represents a condition to test. If any of the conditions represent failure, then a special type of error is signaled which stops execution. If this error is not handled by application-specific code, it is displayed to the user by Shiny.

How is input validated?

Input validation is the process of testing input received by the application for compliance against a standard defined within the application. It can be as simple as strictly typing a parameter and as complex as using regular expressions or business logic to validate input.

What is input parameter validation?

Input Parameter Validation provides the data validation functionality for validating the input requests. You can define your own validation rules for validating different request parameters.

What is data validation in R?

The R package validate facilitates this task by capturing and applying expert knowledge in the form of validation rules: logical restrictions on variables, records, or data sets that should be satisfied before they are considered valid input for further analysis.


1 Answers

You are probably looking for %in%, and you can use it along the lines of:

myFun <- function(input=NULL) {
  Check <- c("ns", "dl", "sv", "asv", "cs")
  if (is.null(input)) {
    message("No 'input' value defined. Using 'ns' by default")
    input <- "ns"
  } 
  if (!input %in% Check) stop("Invalid 'input' value")
  input
}

myFun()
# No 'input' value defined. Using 'ns' by default
# [1] "ns"
myFun("sv")
# [1] "sv"
myFun("vs")
# Error in myFun("vs") : Invalid 'input' value

Without knowing exactly what you want to do, you might also want to look into the switch function.

myFun2 <- function(input = NULL) {
  Check <- c("ns", "dl", "sv", "asv", "cs")
  if (is.null(input)) {
    message("No 'input' value defined. Using 'ns' by default")
    input <- "ns"
  } 
  switch(input,
         ns = "Whoo",
         dl = "Whee",
         sv = "Whaa",
         asv = "Whii",
         cs = "Whuu",
         stop("You did not say the magic word"))
}

myFun2()
# No 'input' value defined. Using 'ns' by default
# [1] "Whoo"
myFun2("sv")
# [1] "Whaa"
myFun2("sc")
# Error in myFun2("sc") : You did not say the magic word

Update: match.arg

By popular demand, here's a match.arg version of the above too, but note that you no longer get to put in a message about not using the magic word and instead have to settle with an automatically generated descriptive and helpful error message. That's no fun....

myFun3 <- function(input=NULL) {
  Check <- c("ns", "dl", "sv", "asv", "cs")
  if (is.null(input)) {
    message("No 'input' value defined. Using 'ns' by default")
    input <- "ns"
  } 
  input <- match.arg(input, Check)
  switch(input,
         ns = "Whoo",
         dl = "Whee",
         sv = "Whaa",
         asv = "Whii",
         cs = "Whuu")
}

myFun3()
# No 'input' value defined. Using 'ns' by default
# [1] "Whoo"
myFun3("sv")
# [1] "Whaa"
myFun3("sc")
# Error in match.arg(input, Check) : 
#   'arg' should be one of “ns”, “dl”, “sv”, “asv”, “cs”
like image 127
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 22 '22 11:09

A5C1D2H2I1M1N2O1R2T1