Function input check is fed into R

I have the following (meaningless) function, in R

:

say <- function (string){
  if(!exists("string")){
    stop("no output string was specified")
  }
  cat(string)
}

      

Everything is well checked for a string object. However, if an object with the same name is already floating on the stage, it will ignore the error, even if it is not defined in the function.

Can I make the exists () function only look at the object-space for the object?

+3


source to share


1 answer


You are looking for missing

. Others do something like this:



say <- function(string=NULL){
  if(is.null(string)){
    stop("no output string was specified")
  }
  cat(string)
}

      

+5


source







All Articles