I have the following (pointless) function, in R
:
say <- function (string){
if(!exists("string")){
stop("no output string was specified")
}
cat(string)
}
Which is all very well at checking the string object actually exists. However, if an object of the same name is already floating about in the workspace, it'll ignore the error even though that's not defined in the function.
Can I make it so the exists() function only looks in the function-space for the object?
You're looking for missing
. Others do something like this instead:
say <- function(string=NULL){
if(is.null(string)){
stop("no output string was specified")
}
cat(string)
}
function(string=NULL)
, so use function(string)
, i.e. no default value - Carl Witthoft 2012-04-06 11:25
if(missing(string)) stop()
jbaums 2012-04-05 23:15