The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
Check arguments and generate readable error messages.
When creating functions for other people to use, you always need to
erify serves the exact purpose.
Install erify from CRAN:
install.packages("erify")
Or install the development version from Github:
# install devtools if not
# install.packages("devtools")
::install_github("flujoo/erify") devtools
Suppose you are creating a function which prints a string several times to emphasize it:
# print `what` `n` times
<- function(what, n) {
emphasize for (i in 1:n) {
cat(what, "\n")
}
}
# example
emphasize("You're beautiful!", 3)
#> You're beautiful!
#> You're beautiful!
#> You're beautiful!
And suppose a novice user accidentally passes a function to argument
what
, he/she will get an error message which is not very
readable:
emphasize(c, 3)
#> Error in cat(what, "\n"): argument 1 (type 'builtin') cannot be handled by 'cat'
You can improve this by adding erify’s check_type()
into
emphasize()
:
<- function(what, n) {
emphasize # check the type of `what`
::check_type(what, "character")
erify
# main
for (i in 1:n) {
cat(what, "\n")
}
}
emphasize(c, 3)
#> Error: `what` must have type character.
#>
#> ✖ `what` has type builtin.
In the above code, check_type(what, "character")
checks
if what
has type character, and if not, generates improved
error message.
You can add more functions to check arguments, customize error messages, and create your own check functions.
See vignette("erify")
for a gentle introduction to
erify.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.