在执行函数之前验证参数

时间:2014-07-23 16:39:21

标签: r function validation arguments

我想验证我传递给函数的两个参数中的每一个都是有效的。

要做到这一点,我要检查一下' State'值我分配给我的状态'变量位于状态列表中,状态列表'。然后我检查一下'测量'价值我分配给我的'衡量'变量在我的度量列表中,' measurelist'。如果第一次检查失败,则无效状态"应生成错误消息。如果第二次检查失败,那么"无效措施"应该生成错误消息。如果两个检查都通过,我希望函数最低< - 函数(状态,测量)运行。

这是我的数据框,'数据':

school<-c("NYU", "BYU", "USC", "FIT", "Oswego","UCLA","USF","Columbia")
state<-c("NY","UT","CA","NY","NY","CA", "CA","NY")
measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(590, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(school,state, measure,score)

此函数检查我是否有有效的州名缩写:

teststate<-function(state){
  statelist<-(c(state.abb,"DC","GU","MP","PR","VI"))
  if(state %in% statelist){
    testmeasure(measure)
  } else{
    stop("invalid state")
  }
 } 

此功能检查我指定的度量是否在我的度量列表中:

testmeasure<-function(measure){ 
    measurelist<-c("MSAT","GPA","VSAT")
    if(measure %in% measurelist){
      lowest(state, measure)
    } else{
      stop("invalid measure")
    }
  }

这是在有效性检查成功后应运行的主要功能:

lowest <- function(state, measure){
    answer<-subset(data,subset=(state==State & measure==Measure))
    order.answer<-order(answer$score,answer$school)
    answer1<-as.matrix(answer[order.answer,])
    answer1[1,1]
  }

对于这个例子:

lowest("NY","MSAT")

正确的答案应该是[1]&#34; Owsego&#34;,但是当我完整地运行代码时,它永远不会完成,控制台只显示:

+ lowest("NY","MSAT")
+ 

当我在一个函数之外独立地测试这些模块时,它们起作用了。

3 个答案:

答案 0 :(得分:2)

试试这个:

lowestV2 <- function(State,Measure){
  ##
  if(State %in% c(state.abb,"DC","GU","MP","PR","VI")){
    if(Measure %in% c("MSAT","GPA","VSAT")){
      answer<-subset(data,subset=(state==State & measure==Measure))
      order.answer<-order(answer$score,answer$school)
      answer1<-as.matrix(answer[order.answer,])
      answer1[1,1]
    } else {
      stop("invalid measure")
    }
  } else {
    stop("invalid state")
  }
  ##
}
##
> lowestV2("NY","MSAT")
[1] "Oswego"

除非您正在捕捉某些类型的复杂情况/条件的错误,否则您可以直接在main函数中包含处理,而不是为每个条件调用外部错误处理函数。

答案 1 :(得分:0)

我相信这个功能可以实现你想要的:

school<-c("NYU", "BYU", "USC", "FIT", "Oswego","UCLA","USF","Columbia")
state<-c("NY","UT","CA","NY","NY","CA", "CA","NY")
measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(590, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(school,state, measure,score)

lowest <- function(State, Measure,Statelist,Measurelist,Data){
  if (!State%in%Statelist){
    stop("invalid state")
  }
  if (! Measure%in%Measurelist){
    stop("invalid measure")
  }
  else{
    answer<-subset(data,subset=(state==State & measure==Measure))
    order.answer<-order(answer$score,answer$school)
    answer1<-as.matrix(answer[order.answer,])
    print(answer1[1,1])
  }
}

lowest("NY","MSAT",state,measure,data)

它还使您能够为函数提供不同的状态,度量和数据集列表,而无需更改函数本身。

答案 2 :(得分:0)

可以使用checkmate包(来自CRAN)进行参数检查。

加载数据:

school <- c("NYU", "BYU", "USC", "FIT", "Oswego","UCLA","USF","Columbia")
stateList <- c("NY","UT","CA","NY","NY","CA", "CA","NY")
measureList <- c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score <- c(590, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data <- data.frame(school, stateList, measureList, score)

加载包,并将测试添加到函数:

library(checkmate)

lowest <- function (State, Measure) {
  expect_subset(State, stateList, empty.ok=FALSE, info="invalid state")
  expect_subset(Measure, measureList, empty.ok=FALSE, info="invalid measure")

  # if you also want to restrict to single states and measures
  assert_atomic(State, len=1)
  assert_atomic(Measure, len=1)

  answer<-subset(data,subset=(stateList==State & measureList==Measure))
  order.answer<-order(answer$score,answer$school)
  answer1<-as.matrix(answer[order.answer,])
  answer1[1,1]
}

测试我们的新功能:

lowest("NY","MSAT")
# [1] "Oswego"
lowest("NJ","MSAT")
# Error with message 'invalid state'
lowest("NY","blah")
# Error with message 'invalid measure'