Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中验证_中的环境的使用_R_Environment_Scoping_Assertthat - Fatal编程技术网

在R中验证_中的环境的使用

在R中验证_中的环境的使用,r,environment,scoping,assertthat,R,Environment,Scoping,Assertthat,我正在尝试编写自己的测试函数(test_if),该函数返回测试结果和可选错误消息。该函数基于assertthat包中该函数的validate_ test_if函数似乎可以工作,但是,我还想在一个更具体的函数(check_input)中使用test_if,该函数分析用户的输入。我有一个问题,如果我在check_输入函数中定义test_if函数,check_输入函数只起作用 我认为这个问题是由搜索范围或环境问题引起的。然而,我对R环境确实是个新手。 我怎样才能在不需要定义测试函数的情况下让我的che

我正在尝试编写自己的测试函数(test_if),该函数返回测试结果和可选错误消息。该函数基于assertthat包中该函数的validate_

test_if函数似乎可以工作,但是,我还想在一个更具体的函数(check_input)中使用test_if,该函数分析用户的输入。我有一个问题,如果我在check_输入函数中定义test_if函数,check_输入函数只起作用

我认为这个问题是由搜索范围或环境问题引起的。然而,我对R环境确实是个新手。 我怎样才能在不需要定义测试函数的情况下让我的check\u输入函数工作呢

非常感谢,西尔克

以下是我的最小工作示例:

library(assertthat)

   test_if <- function(...,msg=NULL) {
   test <- validate_that(...,msg=msg)
     if (is.logical(test)) {

     return(list(assertation=test,msg=NULL))

   }

   if (is.character(test)) {

      return(list(assertation=FALSE,msg=test))
  }


}

test_if(2==3)
test_if(3==3)

test_if(2==3,3==4,msg="something is wrong")


### To check different inputs

check_input1 <- function(value1 = NULL,value2 = NULL) {


  test_if <- function(...,msg=NULL) {

    test <- validate_that(...,msg=msg)

    if (is.logical(test)) {

      return(list(assertation=test,msg=NULL))

    }

    if (is.character(test)) {

      return(list(assertation=FALSE,msg=test))
    }


  }

  error_msg <- ""
  error_status <- FALSE

  check <- test_if(is.numeric(value1))

  error_msg <- check$msg
  error_status <- check$assertation

  return(list(error_msg=error_msg,error_status=error_status))

}


   check_input2 <- function(value1 = NULL,value2 = NULL) {

   error_msg <- ""
   error_status <- FALSE

  check <- test_if(is.numeric(value1))

  error_msg <- check$msg
  error_status <- check$assertation

  return(list(error_msg=error_msg,error_status=error_status))

   }

   check_input1(value1=1)
   check_input2(value1=1)
库(assertthat)
测试