【问题标题】:How does Roxygen to handle infix binary operators (eg. %in%)?Roxygen 如何处理中缀二元运算符(例如 %in%)?
【发布时间】:2012-12-11 23:01:00
【问题描述】:

作为一个简单而具体的例子:

#' 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) )
}

但是,当我尝试构建一个包时,该功能似乎被忽略了,并且没有生成任何文档。

http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Documenting-functions 似乎有一个关于二进制中缀函数的单行简介,但我很难解析它,以及它对 Roxygen 文档的含义。

【问题讨论】:

    标签: r roxygen roxygen2


    【解决方案1】:

    您需要转义使用部分中的%s。另外,我认为您可能需要指定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) )
    }
    

    这是我在个人包中的一个功能。我认为我从未真正使用过该功能,但roxygenize 确实创建了一个帮助文件,并且包通过了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)
    

    【讨论】:

    • @hadley,没有它我会得到一个注释prepare_Rd: PctIn.Rd:4-6: Dropping empty section \usage。除了注释之外,它似乎确实有效。
    • 哦,嗯,我想这是一个逃避问题。我需要完成 roxygen3,然后将其合并回 roxygen2。
    • 描述中的葡萄也需要转义
    【解决方案2】:

    roxygen(说的是roxygen 而不是roxygen2)和中缀运算符我遇到了困难。以下是我的设置(R 2.15.1、roxygen 0.1-3)。

    第一个解决方案:编辑每个中缀运算符的Rd 文件(应该是grapes ... grapes.Rd)并在\alias\usage\nameparts 中的每个% 用一个斜杠转义。

    第二种解决方案:在中缀运算符的文档中指定标签@name 和@usage,并将% 转义。这是一个例子:

    ##' Concatenates two strings
    ##'
    ##' @name \%+\%
    ##' @usage \%+\%(x, y)
    ##' @title Concatenation operator.
    ##' @param a String.
    ##' @param b String.
    ##' @return Same as paste0(a, b).
    "%+%" <- function(a, b) paste(a, b, sep = "")
    

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 2022-11-18
      相关资源
      最近更新 更多