nutterb / argumentcheck Goto Github PK
View Code? Open in Web Editor NEWSimple Tools for Facilitating Informative Parameter Checks in R Functions
Simple Tools for Facilitating Informative Parameter Checks in R Functions
It wouldn't hurt to have a match.arg
method that wouldn't invoke stop
, but I'm not sure if this would work with the case where only one may be used. If there is a failure to match, which option do I use to proceed with the checks?
Or something similar.
It should append the error messages with the text "Function was terminated before all argument checks were complete"
That way I'm at least warning the user that I haven't checked all of their arguments before exiting, indicating the when they correct the current errors, there may be more to come.
The big instance I have in mind is with arguments that are missing. It would be a lot easier to check for all of the missing arguments and terminate when one is found than to have to work in logic of what to do in case of a missing argument. But it's also nice to let the user know that there may be more errors coming.
This is a style thing, but I would tend to want the expr
argument to evaluate to FALSE in order for addError()
and addWarning()
to append a message to the argcheck
object. Because we're "checking" arguments to meet certain logical expressions.
Sometimes we don't necessarily wish to evaluate a logical expression in our error messaging. Other times we want to both evaluate a logical expression and do some other stuff while checking.
JSD <- function(data, gp){
ck <- ArgumentCheck::newArgCheck()
ck <- ArgumentCheck::addError(is.data.frame(data), "'data' must be a data frame", ck)
ck <- ArgumentCheck::addError(length(intersect(class(gp),c("character","factor")))>0,
"'gp' must be either the name of a factor variable in 'data' or a factor",
ck)
finishArgCheck(ck)
n <- nrow(data)
##########################################################
# Note that I had to evaluate 'length(gp)>1' twice here
##########################################################
if(is.character(gp)){
ck <- ArgumentCheck::addWarning(length(gp)>1,
"'gp' is a character vector of length >1. Using only the first element",
ck)
if(length(gp)>1) gp <- gp[1]
}
else if(is.factor(gp)){
ck <- ArgumentCheck::addError(length(gp) == n,
"factor 'gp' must have same length as number of rows in 'data'",
ck)
}
##########################################################
# Note that I had to evaluate 'TRUE' here.
# Maybe making 'expr' an optional parameter would be good.
# Or...what about setting 'expr=TRUE" by default?
##########################################################
else Check <- ArgumentCheck::addError(TRUE,
msg = "'gp' must be a numeric array",
argcheck = Check)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.