我是 R 函数的新手,到目前为止,我会检查参数,例如:
foo = function(state = c("solid", "liquid"),
thing = c("rock", "water")) {
state = match.arg(state)
thing = match.arg(thing)
if (state == "solid" & thing == "water") {
stop("thing can't be water if state is solid!")
}
if (state == "liquid" & thing == "rock") {
stop("thing can't be rock if state is liquid!")
}
}
foo("solid", "brick")
#> Error in match.arg(thing): 'arg' deve ser um dentre "rock", "water"
foo("solid", "water")
#> Error in foo("solid", "water"): thing can't be water if state is solid!
foo("liquid", "rock")
#> Error in foo("liquid", "rock"): thing can't be rock if state is liquid!
使用reprex包(v0.3.0)于2020-06-28创建
但是当处理多个参数时,似乎需要付出很多工作。
我查看了assetthat
和checkmate
包,但对于正确或标准的做法并不清楚。