validate icon indicating copy to clipboard operation
validate copied to clipboard

Using validate within another package

Open DJJ88 opened this issue 4 years ago • 1 comments

Hello,

Many thanks for the validate package, I want to use validate within another package to hide customized rule function from the global environment.

##' @export test
##' @import validate
##' @importFrom methods new
test  <- setRefClass("test",
                     field=list(dt ='data.frame'))

test$methods(
  initialize = function(x){
    dt <<- x
  },
  validate = function(){

    rules <-   validator(y_n = is.yes_no(a))
    summary(confront(.self$dt,rules))
  })


is.yes_no <- function(x){
  tolower(x) %vin% c("yes","no")
}

A minimal script would be

library(test)
df <- data.frame(a =rep(c("yes","NO",NA),4))
obj <- test(df)
obj$validate()

### >
   name items passes fails nNA error warning   expression
1  y_n     0      0     0   0  TRUE   FALSE is.yes_no(a)

and generates an error because is.yes_no is not is scope.

However the configuration where is.yes_no is exported works fine but I'd like to hide it.

##' @export 
is.yes_no <- function(x){
  tolower(x) %vin% c("yes","no")
}

Question: Is it possible to give confront access to is.yes_no without exporting it?

DJJ88 avatar Jul 03 '21 09:07 DJJ88

In part, this is related to #45, where we wish to make it possible for users to register their own validation functions, so you do not need to prepend the function with "is.".

I'm a bit surprised that is.yes_no is not found. That is something I should look into a bit further.

markvanderloo avatar Jul 05 '21 07:07 markvanderloo