Questions tagged [roxygen2]

roxygen2 is a Doxygen-like in-source documentation system for Rd, collation, and NAMESPACE. Its primary use is in documenting R functions in-line with the function definition.

roxygen2 is a -like documentation system for packages; allowing in-source specification of Rd files, collation and namespace directives.

The standard way to write R documentation is to create Rd files (R documentation) in the man directory of a package. Rd files are a special file format which closely resembles . They can be processed into a variety of formats, including LaTeX, and plain text. These files describe each object (function, data set, class, generic or method).

With roxygen2, the documentation is written in comments next to each function. Then an R script is used to create the main files.

Example

#' The length of a string (in characters).
#'
#' @inheritParams str_detect
#' @return numeric vector giving number of characters in each element of the
#' character vector. Missing string have missing length.
#' @keywords character
#' @seealso \code{\link{nchar}} which this function wraps
#' @export
#' @examples
#' str_length(letters)
#' str_length(c("i", "like", "programming", NA))
str_length <- function(string) {
  string <- check_string(string)

  nc <- nchar(string, allowNA = TRUE)
  is.na(nc) <- is.na(string)
  nc
}

Advantages of roxygen2:

  • Code and documentation are adjacent

  • roxygen2 dynamically inspects the objects. For example, it can automatically add links to super and subclasses.

  • it takes care of other files that are fiddly or downright painful to maintain by hand: the namespace, collate order in description, and the demos index.

  • it abstracts over the differences in documenting and methods, generics and classes so that they behave basically the same.

The Rd2roxygen package provides a convenient way of converting Rd files to roxygen comments for existing Rd files.

Repositories

Vignettes

Other resources

Related tags

640 questions
10
votes
1 answer

Prevent line overflow in R documentation?

This is more of an annoyance than it is a problem, but is there any way to prevent the line "overflow" that occurs when documentation in R is compiled and a line is too long? A snippet of some documentation created with R CMD Rd2pdf [options]…
Andy Barbour
  • 8,783
  • 1
  • 24
  • 35
10
votes
1 answer

S3 method help (roxygen2)

I am trying to use a S3 method in a package and thought I understood how to set it up after asking a question here: S3 method consistency warning when building R package with Roxygen But now I get results I don't expect. If I run the code below…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
10
votes
1 answer

How to specify in which order to load S4 methods when using roxygen2

I ran into following problem already multiple times. Say you have two classes, classA and classB described in the following files classA.R : #' the class classA #' #' This is a class A blabla #' \section{Slots}{\describe{\item{\code{A}}{a…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
10
votes
1 answer

Documenting function closures

Suppose I have a function closure in my package, for example f = function(x) { x = x g = function(y) x <<- y h = function() x list(g = g, h = h) } l = f(5) l$g(10) l$h() What is the correct (in the official CRAN sense) way of…
csgillespie
  • 59,189
  • 14
  • 150
  • 185
9
votes
2 answers

Finding objects from other packages' namespaces in package code

I'm refactoring a package that imports many other packages' full namespaces. I believe that many of these dependencies are used for single function call uses that would be better handled using importFrom, or are orphaned dependencies that are no…
bcarlsen
  • 1,381
  • 1
  • 5
  • 11
9
votes
2 answers

R package fails devtools::check, because "could not find function" even though the function is imported in NAMESPACE

Trying to build my first R package using roxygen2 and devtools. I have added a function that uses %>% and mutate in the @examples section. When I run check() it fails, because it cannot find the function %>% or mutate. Based on this, this, and this…
Andrew
  • 490
  • 3
  • 9
9
votes
1 answer

NAMESPACE option created by RcppArmadillo.package.skeleton causes error

I'm creating a R package that contains Rcpp functions depending on RcppArmadillo, so I first generate the package skeleton by RcppArmadillo.package.skeleton. However, when I roxygenize my package, it gives me an error shown below. R…
inmybrain
  • 386
  • 3
  • 16
9
votes
0 answers

R: Documenting ellipsis arguments with roxygen2 in S4 generic

I have a number of functions that have related methods with different arguments that I want to document together within the generic. Is there a standard method for documenting arguments passed through an ellipsis to the different methods by an S4…
ssokolen
  • 468
  • 3
  • 13
9
votes
2 answers

Insert markdown table in roxygen2 R package documentation

I'm writing an R package and I want to include a table in an R help file, e.g. in the @details section. I tried including markdown code directly: #' | Tables | Are | Cool | #' | ------------- |:-------------:| -----:| #' | col 3 is…
needRhelp
  • 2,948
  • 2
  • 24
  • 48
9
votes
2 answers

Inherit Roxygen2 documentation for multiple arguments in R package

I'm writing an R package and want to inherit the documentation for two arguments (say x and y) from an old function (say old()) to a new function, new(). The twist is that these two arguments share the same argument description. That is, in old(),…
Simon Jackson
  • 3,134
  • 15
  • 24
9
votes
2 answers

C++ function not available

I have the following file cumsum_bounded.cpp #include using namespace Rcpp; //' Cumulative sum. //' @param x numeric vector //' @param low lower bound //' @param high upper bound //' @param res bounded numeric vector //' @export //'…
jwenzeslaus
  • 125
  • 7
9
votes
1 answer

Exported function not exported correctly

I am developing an R package (let's call it pkg), and I have defined a few generics. The NAMESPACE file contains both S3method(foo, bar) export(foo) However, upon calling pkg::foo.bar, I get the dreaded Error: 'foo.bar' is not an exported object…
JohnA
  • 321
  • 2
  • 11
9
votes
2 answers

Can I automatically generate unit tests for testthat from roxygen2 examples?

I'm working on the PKNCA package for R. While developing the testing code, some of the tests would also be good examples. I want to keep them as both (test and example). Is there a way that I can embed something within the roxygen2 documentation…
Bill Denney
  • 766
  • 1
  • 6
  • 21
9
votes
1 answer

Location of files when using @example tag with roxygen2

When documenting a function with roxygen2 it is possible to place examples in a separate file. See here: http://r-pkgs.had.co.nz/man.html "Instead of including examples directly in the documentation, you can put them in separate files and use…
waferthin
  • 1,582
  • 1
  • 16
  • 27
9
votes
1 answer

Automatically document all methods of an S4 generic, using roxygen2

I am writing an r package using roxygen2 for the documentation. I am having some trouble documenting S4 methods. I have defined a generic s4 method (e.g. myGeneric) and few methods that implement it. Question: Is there a way to automatically…
alko989
  • 7,688
  • 5
  • 39
  • 62