f <- function(x, ...) {
return(
as.list(substitute(list(...)))[-1]
)
}
err <- f(x = 0, y = 10,) #have a closer look --> str(err)
#$`y`
#[1] 10
#
#[[2]]
fine<- f(x = 0, y = 10)
#$`y`
#[1] 10
В случае ошибки вы видите пустой элемент списка символов. Я полагаю, что вы можете использовать эту информацию как ловушку для обработки ошибок.
Так что ваша значимая обработка ошибок может выглядеть так:
Спасибо @Roland за участие.
f <- function(x, ...) {
res <- as.list(substitute(list(...)))[-1]
if( any( sapply(res, function(x) { is.symbol(x) && deparse(x) == "" }) ) ) stop('Please remove the the last/tailing comma "," from your input arguments.')
print("code can run. Hurraa")
}
a <- 0;
f(x = 0, y = a, hehe = "", "")
#[1] "code can run. Hurraa"
f(x = 0, y = a, hehe = "", "", NULL)
#[1] "code can run. Hurraa"
f(x = 0, y = a, hehe = "", "",)
#Error in f(x = 0, y = a, hehe = "", "", ) :
#Please remove the the last/tailing comma "," from your input arguments.
f(x = 0, y = a, hehe = "", "", NULL,)
#Error in f(x = 0, y = a, hehe = "", "", NULL, ) :
#Please remove the the last/tailing comma "," from your input arguments.