roxygen2 and help files
In addition to writing tutorials that give an overview of your whole package, you should also write specific documentation showing users how to use and interpret any functions you expect users to directly call.
These help files will ultimately go in a folder called /man of your package, in an R documentation format (.Rd file extensions) that is fairly similar to LaTex. You used to have to write all of these files as separate files. However, the roxygen2 package lets you put all of the help information directly in the code where you define each function.
With roxygen2, you add the help file information directly above the code where you define each functions, in the R scripts saved in the R subdirectory of the package directory. You start each line of the roxygen2 documentation with # ‘ (the second character is an apostrophe, not a backtick).
The first line of the documentation should give a short title for the function, and the next block of documentation should be a longer description. After that, you will use tags that start with @ to define each element you’re including. You should leave an empty line between each section of documentation, and you can use indentation for second and later lines of elements to make the code easier to read.
Here is a basic example of how this roxygen2 documentation would look for a simple “Hello world” function:
1#' Print "Hello world"
2#'
3#' This is a simple function that, by default, prints "Hello world". You can
4#' customize the text to print (using the \code{to_print} argument) and add
5#' an exclamation point (\code{excited = TRUE}).
6#'
7#' @param to_print A character string giving the text the function will print
8#' @param excited Logical value specifying whether to include an exclamation
9#' point after the text
10#'
11#' @return This function returns a phrase to print, with or without an
12#' exclamation point added. As a side effect, this function also prints out
13#' the phrase.
14#'
15#' @examples
16#' hello_world()
17#' hello_world(excited = TRUE)
18#' hello_world(to_print = "Hi world")
19#'
20#' @export
21hello_world <- function(to_print = "Hello world", excited = FALSE){
22 if(excited) to_print <- paste0(to_print, "!")
23 print(to_print)
24}
Including data in a package
Many R packages are designed to manipulate, visualize, and model data so it may be a good idea for you to include some data in your package. The primary reason most developers include data in their package is to demonstrate how to use the functions included in the package with the included data. Creating a package as a means to distribute data is also a method that is gaining popularity. Additionally you may want to include data that your package uses internally, but is not available to somebody who is using your package. When including data in your package consider the fact that your compressed package file should be smaller than 5MB, which is the largest package size that CRAN allows. If your package is larger than 5MB make sure to inform users in the instructions for downloading and installing your package.