microbe
microbe

Reputation: 2169

R checking a parameter is defined

I'm looking for a general practice how to check a parameter was defined in a function. I came up with these three ideas. Which one is the proper way of doing it?

Unfortunately, the third one is not working. substitute() is working differently in a function and I couldn't figure it out how to use it properly.

file.names <- list(
                   cov.value <- "cov.rds",
                   plot.name <- "plot.pdf"
)

test1 <- function(file.names){
  is.save <- !missing(file.names)
}

test2 <- function(file.names = NULL) {
  is.save <- !is.null(file.names)
}

test3 <- function(file.names = NULL) {
  is.save <- exists(as.character(substitute(file.names)))
}

Upvotes: 4

Views: 2427

Answers (2)

Tommy
Tommy

Reputation: 40821

I personally think the second approach with a default value is MUCH easier to use & understand. (And the third approach is just really bad)

...especially when you are writing a wrapper function that needs to pass an argument to it. How to pass a "missing"-value is not obvious!

wraptest1 <- function(n) {
    file.names <- if (n > 0) sample(LETTERS, n) 
      else alist(a=)[[1]] # Hacky way of assigning 'missing'-value
    print(test1(file.names))
}
wraptest1(2) # TRUE
wraptest1(0) # FALSE

wraptest2 <- function(n) {
    file.names <- if (n > 0) sample(LETTERS, n) 
      else NULL # Much easier to read & understand
    print(test2(file.names))
}
wraptest2(2) # TRUE
wraptest2(0) # FALSE

[granted, there are other ways to work around passing the missing value, but the point is that using a default value is much easier...]

Some default values to consider are NULL, NA, numeric(0), ''

Upvotes: 3

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

It's generally a good idea to look at code from experienced coders---and R itself has plenty of examples in the R sources.

I have seen both your first and second example being used. The first one is pretty idiomatic; I personally still use the second more often by force of habit. The third one I find too obscure.

Upvotes: 2

Related Questions