Search code examples
rroxygenroxygen2

How does Roxygen to handle infix binary operators (eg. %in%)?


As a simple, concrete example:

#' Inverse Value Matching
#' 
#' Complement of \code{%in%}. Returns the elements of \code{x} that are
#' not in \code{y}.
#' @usage x %nin% y
#' @param x a vector
#' @param y a vector
#' @export
"%nin%" <- function(x, y) {
  return( !(x %in% y) )
}

However, when I attempt to build a package the function seems to be ignored and no documentation is generated.

There seems to be a one-line blurb about binary infix functions at http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Documenting-functions, but I am having a hard time parsing it, and what it would imply for Roxygen documentation.


Solution

  • You need to escape the %s in the usage section. Also, I think you may need to specify an rdname

    #' Inverse Value Matching
    #' 
    #' Complement of \code{%in%}. Returns the elements of \code{x} that are
    #' not in \code{y}.
    #' @usage x \%nin\% y
    #' @param x a vector
    #' @param y a vector
    #' @export
    #' @rdname nin
    "%nin%" <- function(x, y) {
      return( !(x %in% y) )
    }
    

    Here is a function I have in a personal package. I don't think I've ever actually used the function, but roxygenize does create a help file and the package passes R CMD check.

    #' percent in
    #' 
    #' calculate the percentage of elements of \code{table} that are in \code{x}
    #' 
    #' @param x vector or NULL: the values to be matched
    #' @param table vector or NULL: the values to be matched against
    #' @return percentage of elements of \code{x} that are in \code{table}
    #' @author gsee
    #' @usage x \%pctin\% table
    #' @examples
    #' letters[1:10] %pctin% letters[1:3] # 30% of the second arg ar in the first
    #' @export
    #' @rdname PctIn
    "%pctin%" <- function(x, table) length(x[x %in% table])/length(x)