moodymudskipper / tags

A collection of tags built using the package tag
GNU General Public License v3.0
8 stars 0 forks source link

checking_args #19

Closed moodymudskipper closed 5 years ago

moodymudskipper commented 5 years ago

functions don't always do all the checks we'd want, we can do them separately but it's verbose and checks aren't directly connected to the function call.

checking_args(na.rm = is.logical)$mean(1:3, na.rm = na.rm)

We could think of how to incorporate custom errors by entering a list of length 2 with the above as the first element and an error text as a second element, but let's think more first because if we complexify the function we might want to let user choose between warnings or errors.

Something like

checking_args(na.rm = list(warn = is.logical, text = "na.rm will be coerced to logical")$mean(1:3, na.rm = na.rm)

text could also be a function (or formula) of the given value

checking_args(na.rm = list(warn = is.logical, ~ sprintf("na.rm is of type %s and will be coerced to logical", typeof(.))))$mean(1:3, na.rm = na.rm)
moodymudskipper commented 5 years ago

Actually using tracing we can do :

tracing(is.logical(na.rm) || warn(sprintf("na.rm is of type %s and will be coerced to logical", typeof(.))))$mean(1:3, na.rm = foo)

which is more flexible and reads better for complex cases., so we can keep the simple version, and suggest the syntax above in the help file.

moodymudskipper commented 5 years ago

maybe better use enclosing than tracing in this case as we are not updating these values

moodymudskipper commented 5 years ago

done, with slightly different implementation