【发布时间】:2015-07-13 09:43:17
【问题描述】:
在编写函数时,检查参数的类型很重要。例如,采用以下(不一定有用)正在执行子集的函数:
data_subset = function(data, date_col) {
if (!TRUE %in% (is.character(date_col) | is.expression(date_col))){
stop("Input variable date is of wrong format")
}
if (is.character(date_col)) {
x <- match(date_col, names(data))
} else x <- match(deparse(substitute(date_col)), names(data))
sub <- data[,x]
}
我想允许用户提供应提取为字符或表达式的列(例如,名为“日期”的列与仅日期)。一开始我想检查 date_col 的输入是否真的是字符值或表达式。但是,'is.expression' 不起作用:
Error in match(x, table, nomatch = 0L) : object '...' not found
由于 deparse(substitute)) 可以提供表达式,因此我认为“is.expression”也必须有效。 这里有什么问题,谁能给我提示?
【问题讨论】:
标签: r function arguments expression