When building packages that are used by other people, is there a best practice in the R community for validating the inputs provided to a function?
For example, let's say I have a function that will only provide a meaningful result if the input is an integer vector. If the input contains non-integer numeric values, the results will not be accurate or useful.
Let's assume that the documentation clearly specifies that the input to the function should be an integer vector. I can think of three options for how to proceed.
- My function should do nothing and assume that the inputs are provided correctly.
- My function should validate the inputs by checking the type. If the expectations are not met, the function can raise an error.
- My function should validate the inputs by checking the type. If the expectations are not met, the function can attempt to change the input (e.g. convert from numeric to integer).
I do not like option 3, since that type of input transformation should be done by the user. However, I can see arguments for both option 1 and option 2. Is there a preference in the R community?
Thanks for your help