poissonconsulting / chk

An R package for developers to check user-supplied function arguments
https://poissonconsulting.github.io/chk/
Other
48 stars 4 forks source link

How do you apply functions on vectors? #99

Closed harell closed 3 years ago

harell commented 3 years ago

Hi

Love your work on the chk package, one question please.

Say I have a vector account_id that has contain whole numbers, what is an elegant way to test it?

I could run one element at a time for example:

 purrr::walk(account_id, chk::chk_whole_number)

but then when an error occurs, the var name is obscured

Error: `.x[[i]]` must be a whole number (non-missing integer scalar or double equivalent).

I can overcome it by specifying the var name

purrr::walk(event_time, chk::chk_whole_number, x_name = "account_id")

but this solution is not elegant. Importantly, it is cumbersome to implement when there are many vectors to check, e.g. a data frame.

Also, the error modifies the var name (notice capital 'A' in the error, while the original name starts with lower case 'a')

Error: Account_id must be a whole number (non-missing integer scalar or double equivalent).
joethorley commented 3 years ago

Hi @harell

Thanks for your feedback.

chk_whole_numeric() is the function you want to use to test if a vector consists of whole numbers

Note chk_integer() checks if a vector is of class integer.

Also to preserve the case of x_name add backticks

> chk::chk_true(1, x_name = "Account_Id")
Error: Account_Id must be TRUE.
chk::chk_true(1, x_name = "`Account_Id`")
Error: `Account_Id` must be TRUE.
github-actions[bot] commented 2 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.