0

I've been trying to create an error message when the ouput entered is wrong, for example, in this code instead of entering 4 digits number, it is entered a character.

I keep receiving an error. Any tips?

get_age <- function() {
  yob <- readline("Please enter your year of birth: ")
  age <- 2022 - as.numeric(yob)
  return(age)
}
  if (get_age != as.numeric(yob)) { 
  withCallingHandlers(
    warning = function(cnd){
      readline("This is not a number. Please, try again.")
    },
    print("please, enter a numerical value"), 
return(get_age())
  )
}
  • You need `tryCatch()`, Have a look at [this](https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r) question. – maydin Oct 21 '22 at 10:26

0 Answers0