Я уверен, что вы ищете deparse(substitute(x))
и get()
здесь.Первый преобразует указанные имена в строки, второй загружает ваши данные в первую очередь.Для исключения мы можем просто использовать выражение if
.
Val_Uniques <- function(df, col) {
df <- deparse(substitute(df))
df <- get(df)
col <- deparse(substitute(col))
if(!(col %in% names(df)))
stop("the column does not exist")
return(unique(df[[col]]))
}
Тест
> Val_Uniques(mytable, city)
[1] A D B C E
Levels: A B C D E
> Val_Uniques(mytable, foo)
Error in Val_Uniques(mytable, foo) : the column does not exist
Данные
mytable <- data.frame(city=LETTERS[c(1, 4, 4, 2, 3, 2, 5, 4)],
var=c(1, 3, 22, 4, 5, 8, 7, 9))