11 Function documentation
The special commented section seen in the previous example will be used by a package called roxygen2
. We have to follow this exact syntax, so that this package can automatically build a really neat documentation of our package for us. Let’s try to understand its basic structure. For reference, these were the different parts:
- A small description of the function, nothing else.
#' Create a new dataframe where each row has a year range into one where each
#' row is a single year, effectively 'expanding' the whole year range
- A small description of each parameter the function receives. It should be like:
#' @param param_name_1 Description of param 1
#' @param param_name_2 Description of param 2
#' ...
As you see here I think it is OK to add line breaks in between, as long as each parameter starts with @param
.
#' @param trade_sources A tibble dataframe
#' where each row contains the year range
- A small description of the value the function returns. It should start with
@returns
.
#' @returns A tibble dataframe where each row
#' corresponds to a single year for a given source
- A simple line containing
@export
to indicate the function can be used in the package, i.e., it is public.
#' @export
- A ‘code’ section of examples to illustrate the function’s behavior. It must start with
@examples
, and after that you can write usual R code. When this is processed, it automatically runs the code and adds some lines with its output in the documentation.
#' @examples
#' trade_sources <- tibble::tibble(
#' Name = c("a", "b", "c"),
#' Trade = c("t1", "t2", "t3"),
#' Info_Format = c("year", "partial_series", "year"),
#' Timeline_Start = c(1, 1, 2),
#' Timeline_End = c(3, 4, 5),
#' Timeline_Freq = c(1, 1, 2),
#' `Imp/Exp` = "Imp",
#' SACO_link = NA,
#' )
#' expand_trade_sources(trade_sources)
These options are enough to get us started with a nice documentation. In the Writing articles section we will learn how to generate and see this documentation. In this example, it would look something like this (note the autogenerated example code output):