Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roxygen2: document a function with an optional parameter

Tags:

r

roxygen2

What is the correct way of roxygen documenting a function with an optional parameter like

#' @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)
}

In particular how should one comment on the optional parameters chambers and hgt?

like image 546
Manfredo Avatar asked Oct 19 '16 21:10

Manfredo


People also ask

What is an optional parameter in a function?

What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.

What is .RD file in R?

R objects are documented in files written in “R documentation” (Rd) format, a simple markup language much of which closely resembles (La)TeX, which can be processed into a variety of formats, including LaTeX, HTML and plain text.


1 Answers

I would just add a @param field for each argument and explicitly write if an argument is optional, like this:

#' @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)
}

If your user reads docs, then he/she would know that these arguments are optional. If not, he/she will figure it out experimentally by omitting these arguments.

Hope this helps.

P.S. Good R coding practice requires you to document each and every function argument. If you don't do this, Roxygen2 would issue warnings during the package check.

like image 181
R Kiselev Avatar answered Sep 22 '22 16:09

R Kiselev