I'm trying to write some code that does all of the following:
- At run time, asks, with an actual question (e.g. "How many PCs do you have? ") the user to type in an integer.
- Check if the user has given either an object of integer type or something that can be coerced in to that without error. This step must not require any sort of
grep(e.g. that makes this solution invalid). - Instead of giving any warning or errors if the input wasn't an integer, ask a new question that is repeated until they do give an integer (e.g. ask "Try again? " until an integer is given).
- Outputs this integer.
The solutions to similar questions appear to be to use either scan or readlines. I've ruled out scan because it likes to throw its own error messages, needs help from print to ask questions, and will ask for another input if given an invalid one even if you've told it to only try for one (e.g. scan(n = 1,quiet = TRUE) will ask for another input if you give it a bunch of letters). I've found that readlines is almost satisfactory, but because readlines treats its input as a string, I've been unable to write atrycatch block that satisfies requirement #3.
What are my options? It's looking like I've missed a simple solution.