如何使用 Roxygen 正确记录来自不同包的泛型的 S3 方法?
我正在编写一个包,它定义了一个新类、surveyor 和一个为此的 print 方法,即 print.surveyor。我的代码工作正常,我使用 roxygen 进行内联文档。但是 R CMD check 发出警告:
使用的函数/方法 文档对象“print.surveyor” 但不在代码中: print
我使用了 Hadley 编写的以下两页作为灵感: 命名空间 和 记录函数,两者都指出正确的语法是 @method function-name class
所以我的问题是:什么是记录的正确方法使用 Roxygen 为我的新类添加 print
方法?更具体地说,我如何摆脱警告?
这是我的代码:(注释文档表明尝试修复此问题,但没有一个起作用。)
#' Prints surveyor object.
#'
#' Prints surveyor object
#'
## #' @usage print(x, ...)
## #' @aliases print print.surveyor
#' @param x surveyor object
#' @param ... ignored
#' @S3method print surveyor
print.surveyor <- function(x, ...){
cat("Surveyor\n\n")
print.listof(x)
}
以及 roxygenized 输出,即 print.surveyor .Rd
:
\name{print.surveyor}
\title{Prints surveyor object.}
\usage{print(x, ...)
#'}
\description{Prints surveyor object.}
\details{Prints surveyor object
#'}
\alias{print}
\alias{print.surveyor}
\arguments{\item{x}{surveyor object}
\item{...}{ignored}}
I am writing a package that defines a new class, surveyor, and a print
method for this, i.e. print.surveyor
. My code works fine and I use roxygen for inline documentation. But R CMD check
issues a warning:
Functions/methods with usage in
documentation object 'print.surveyor'
but not in code: print
I have used the following two pages, written by Hadley, as inspiration:
Namespaces and Documenting functions, both of which states that the correct syntax is @method function-name class
So my question is: What is the correct way of documenting the print
method for my new class using Roxygen? And more specifically, how do I get rid of the warning?
Here is my code: (The commented documentation indicated attempts at fixing this, none of which worked.)
#' Prints surveyor object.
#'
#' Prints surveyor object
#'
## #' @usage print(x, ...)
## #' @aliases print print.surveyor
#' @param x surveyor object
#' @param ... ignored
#' @S3method print surveyor
print.surveyor <- function(x, ...){
cat("Surveyor\n\n")
print.listof(x)
}
And the roxygenized output, i.e. print.surveyor.Rd
:
\name{print.surveyor}
\title{Prints surveyor object.}
\usage{print(x, ...)
#'}
\description{Prints surveyor object.}
\details{Prints surveyor object
#'}
\alias{print}
\alias{print.surveyor}
\arguments{\item{x}{surveyor object}
\item{...}{ignored}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新
截至 roxygen2 > 3.0.0 该软件包在为您解决所有这些问题方面变得更加智能。您现在只需要
@export
标签,roxygen 就会计算出什么您正在记录的事情,并在转换期间编写NAMESPACE
等时执行适当的操作。在某些例外情况下,您可能需要帮助roxygen。 Hadley Wickham 在他的 R 包中使用的示例 书是
all.equal.data.frame
。该函数名称对于什么是类以及什么是通用函数(all
、all.equal
或all.equal.data< /代码>)?
在这种情况下,您可以通过显式告知 roxygen 泛型和类/方法来帮助它,例如,
如果您需要显式使用 @method,下面的原始答案解释了有关旧行为的更多信息。
原始
函数应使用
@method
标签进行记录:在初次阅读时,@hadley 的文档对我来说有点混乱,因为我不熟悉 roxygen,但经过几次阅读后阅读该部分的内容后,我想我理解您需要
@method
的原因。您正在为
print
方法编写完整文档。@S3method
与NAMESPACE
相关,并安排要导出的方法。@S3method
并不用于记录方法。您的 Rd 文件的
usage
部分应包含以下内容:如果工作正常,因为这是在 Rd 文件中记录 S3 方法的正确方法。
Update
As of roxygen2 > 3.0.0 the package has gotten a lot smarter at figuring all this out for you. You now just need the
@export
tag and roxygen will work out what kind of thing you are documenting and do the appropriate thing when writing theNAMESPACE
etc during conversion.There are exceptions where you may need to help out roxygen. An example that Hadley Wickham uses in his R Packages book is
all.equal.data.frame
. There is ambiguity in that function name as to what is the class and what is the generic function (all
,all.equal
, orall.equal.data
)?In such cases, you can help roxygen out by explicitly informing it of the generic and class/method, e.g.
The original answer below explains more about the older behaviour if you need to explicitly use
@method
.Original
The function should be documented with the
@method
tag:On initial reading, @hadley's document was a little confusing for me as I am not familiar with roxygen, but after several readings of the section, I think I understand the reason why you need
@method
.You are writing full documentation for the
print
method.@S3method
is related to theNAMESPACE
and arranges for the method to be exported.@S3method
is not meant for documenting a method.Your Rd file should have the following in the
usage
section:if this works correctly, as that is the correct way to document S3 methods in Rd files.
就roxygen2而言, 3.0.0.,您只需要
@export
因为 roxygen 可以确定print.surveyor
是一个 S3 方法。这意味着您现在只需要然而,在这种情况下,由于文档不是很有用,最好只执行以下操作:
As of roxygen2 > 3.0.0., you only need
@export
because roxygen can figure out thatprint.surveyor
is an S3 method. This means that you now only needHowever, in this case since the documentation isn't very useful, it'd probably better to just do:
@export
仅在加载泛型时才有效。如果泛型位于另一个包中,则需要导入泛型。 块来解决此问题在当前的 roxygen 中,可以使用来自 dplyr/ 的 R/reexport-tibble.r。在此示例中,从 tibble 包导入
data_frame
方法,并导出 tibble::data_frame。然后,此类重新导出的对象将记录在reexports.Rd
文件中,不用说,该文件满足 R CMD 检查。@export
only works if the generic is loaded. If the generic is in another package you need to import the generic. With current roxygen this is solved with a block liketaken from dplyr/R/reexport-tibble.r . In this example, the
data_frame
method is imported from the tibble package, and tibble::data_frame is exported. Such re-exported objects are then documented in areexports.Rd
file that - needless to say - satisfies R CMD check.