Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools for input checking in functions #149

Closed
Bisaloo opened this issue Oct 4, 2021 · 7 comments · Fixed by #150
Closed

Tools for input checking in functions #149

Bisaloo opened this issue Oct 4, 2021 · 7 comments · Fixed by #150

Comments

@Bisaloo
Copy link
Member

Bisaloo commented Oct 4, 2021

This follows a discussion on our institute's slack workspace with @pearsonca, @seabbs, @TimTaylor.

What are the best practices for input checking in functions? This is an important question since R doesn't have an explicit type system (for now?).

  • the base solution stop()/stopifnot(). stopifnot() can also take named argument to produce nicer error messages. It's also possible to write your own function if you end up testing the same kind of things many times.
  • assertthat which seems to have been superseded by testthat (when used outside the testing framework)
  • vctrs::vec_assert() which also works with data.frames
  • checkmate
  • check

check's README also mentions other alternatives that didn't come up in the discussion:

The popularity of each of these packages (as measured by the number of GitHub stars) makes a good case that such a blog post would be appreciated by the community 😄

@Bisaloo
Copy link
Member Author

Bisaloo commented Oct 4, 2021

Also a good place to talk about https://github.com/ropensci-review-tools/autotest as a way to control you check your inputs correctly?

@maelle
Copy link
Member

maelle commented Oct 5, 2021

Oh yeah that's an excellent topic! Incidentally yesterday I was looking at the mlr dev docs and they mentioned checkmate.

Interested in contributing this post @Bisaloo et al?

@maelle
Copy link
Member

maelle commented Oct 12, 2021

Maybe relevant https://github.com/djnavarro/bs4cards/blob/master/R/validators.R

@TimTaylor
Copy link

TimTaylor commented Oct 13, 2021

Looks like rlang::abort() is having quite an update in the next version. Not sure when this is likely to land but currently it no longer hides the call by default. This has knock on effects with assertions in vctrs as they no longer hide the call by default in an RStudio session

library(vctrs)
f <- function(x) vec_assert(x, integer())
f(1)
#> Error in `vec_assert()`: `x` must be a vector with type <integer>.
#> Instead, it has type <double>.

Anyone have an idea of where they are in their release timeline so the post does not go out of date too quick? 😅
Edit - previous error would have been

#> Error: `x` must be a vector with type <integer>.
#> Instead, it has type <double>.

@Bisaloo
Copy link
Member Author

Bisaloo commented Oct 13, 2021

Speaking of rlang, this might be relevant as well: r-lib/rlang#1111

@gaborcsardi
Copy link
Contributor

FYI a new rlang release is in the works AFAIK.

@seabbs
Copy link
Contributor

seabbs commented Oct 16, 2021

For reference: https://twitter.com/krlmlr/status/1449470169303195651?s=21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants