R checking a parameter is defined
function, parameter-passing, r
Solution
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)`, `''`
Problem
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))) } ```