I'm writing a function in R that takes either a data.frame or a file path, leading to a csv that would be read in as a data.frame.
I currently have this:
occInput <- function(fileInput, dfInput = NA) {
if (is.na(dfInput)) occ.dat <- read.csv(file = fileInput)
else occ.dat <- dfInput
#manipulate the data in occ.dat
}
However, when I actually pass in a data.frame as the dfInput parameter, it throws the warning:
Warning in
if (is.na(dfInput)) occ.dat <- read.csv(file = fileInput) else occ.dat <- dfInput: the condition has length > 1 and only the first element will be used
While this does not actually adversely affect my code, it's ugly, and it suggests to me that there is a more elegant way to have either/or optional arguments in a function.
Any suggestions? I feel like I'm overlooking something obvious.
is.null(dfInput)is an better option.