用可选参数记录函数的正确方法是什么,如
#' @name dbh2vol
#' @usage dbh2vol(dbh,ipft)
#' @description This is an allometric function to return the tree volume
#' @param dbh diameter at breast height
#' @param ipft PFT
#' @return vol volume
#' @export
dbh2vol <- function(dbh,ipft,...,hgt, chambers = FALSE){
if (missing(hgt)) hgt = other_function (dbh, ipft)
vol = hgt * dbh ^ pft$vol[ipft]
if (chambers) vol = vol * 2
return(vol)
}特别是如何评论可选参数chambers和hgt
发布于 2017-08-03 09:25:18
我只需为每个参数添加一个@param字段,如果参数是可选的,则显式写入,如下所示:
#' @name dbh2vol
#' @usage dbh2vol(dbh,ipft)
#' @description This is an allometric function to return the tree volume
#' @param dbh diameter at breast height
#' @param ipft PFT
#'
#' @param chambers optional parameter that is blah-blah... FALSE by default
#' @param hgt function to do this and that (optional).
#' If not provided, \code{other_function(dbh, ipft)} is used.
#'
#' @return vol volume
#' @export
dbh2vol <- function(dbh,ipft,...,hgt, chambers = FALSE){
if (missing(hgt)) hgt = other_function (dbh, ipft)
vol = hgt * dbh ^ pft$vol[ipft]
if (chambers) vol = vol * 2
return(vol)
}如果用户读取文档,那么他/她就会知道这些参数是可选的。如果没有,他(她)将通过省略这些论点来从实验上解决这个问题。
希望这能有所帮助。
P.S.良好的R编码实践要求您记录的每个函数参数。如果不这样做,Roxygen2将在包检查期间发出警告。
https://stackoverflow.com/questions/40141604
复制相似问题