【发布时间】:2018-05-17 21:42:38
【问题描述】:
我正在 R 中编写一个函数,该函数采用 data.frame 或一个文件路径,生成一个 csv,该 csv 将作为 data.frame 读入。
我目前有这个:
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
}
但是,当我实际将 data.frame 作为 dfInput 参数传入时,它会引发警告:
if (is.na(dfInput)) occ.dat <- read.csv(file = fileInput) else occ.dat <- dfInput中的警告:条件的长度 > 1 并且只有 将使用第一个元素
虽然这实际上不会对我的代码产生不利影响,但它很丑陋,而且它向我表明,有一种更优雅的方法可以在函数中包含一个/或可选参数。
有什么建议吗?我觉得我忽略了一些明显的东西。
【问题讨论】:
-
我认为检查
is.null(dfInput)是一个更好的选择。