我是 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 年 6 月 28 日创建
但是在使用多个 args 时似乎需要做很多工作。
我看了看assetthat
和checkmate
包装,但我不清楚什么是正确或标准的方法。