vendredi 24 mai 2019

Creating a function message in R that tests if the input is a string

I want to make a message in my function that tells the user to pass in the argument as a string, that is if they forget the quotation marks then the message should say "use quotation marks" and not "object X not found". Is that possible?

print_name <- function(name){
  if (!is.character(name)){
    stop("Name should be a character")
  }
  else {
    print(name)
  }
}

print_name(david)
#> Error in print_name(david): object 'david' not found

print_name("david")
#> [1] "david"

Created on 2019-05-24 by the reprex package (v0.2.1)

Aucun commentaire:

Enregistrer un commentaire