Search code examples
rdocumentationr-packageroxygen2binary-operators

roxygen2 documentation of binary operators - including examples causes warning or error on check


I try to include binary operators in my R packge and of course want to document them properly. After stuying How does Roxygen to handle infix binary operators (eg. %in%)? I tried the accepted solutions variant, but I have a question. If I replicate this in a 1:1 manner and want to apply that for my own package, and apply @GSee's approach then this works as long as I don't bring in the @examples tag. Strangely @example works, but drops a warning during the package check procedure in RStudio.

Here is what I tried: first example without @example or @examples: no issue

#' @title
#' Inverse Value Matching
#'
#' @description
#' 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
#'
#' @export
# -----------------------------------------------------------------------------
"%nin%" <- function(x, y) {
  return( !(x %in% y) )
}
# -----------------------------------------------------------------------------

now with @example, passes the check but drops a warning.

#' @title
#' Inverse Value Matching
#'
#' @description
#' 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
#'
#' @export
#' @example c(1:3) \%nin\% c(3:5)
# -----------------------------------------------------------------------------
"%nin%" <- function(x, y) {
  return( !(x %in% y) )
}
# -----------------------------------------------------------------------------

finally with @examples, fails the check

#' @title
#' Inverse Value Matching
#'
#' @description
#' 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
#'
#' @export
#' @examples 
#' c(1:3) \%nin\% c(3:5)
# -----------------------------------------------------------------------------
"%nin%" <- function(x, y) {
  return( !(x %in% y) )
}
# -----------------------------------------------------------------------------

What am I doing wrong?


Solution

  • So, after spending a day with it, the solution :) .

    The tag @export must be followed by the operator name in double quoutes, and as @stefan pointed out, the \ were superflous. Thanks, and enjoy the solution for your projects.

    #' @title
    #' Inverse Value Matching
    #'
    #' @description
    #' 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%"
    #' @examples
    #' Test::`%nin%`(c(1:3), c(3:5)) # result: 1 2
    #' c(1:3) %nin% c(3:5)
    # -----------------------------------------------------------------------------
    "%nin%" <- function(x, y) {
      return( !(x %in% y) )
    }
    # -----------------------------------------------------------------------------