如何使用 Roxygen 正确记录来自不同包的泛型的 S3 方法?

发布于 2024-11-18 03:54:14 字数 1262 浏览 2 评论 0原文

我正在编写一个包,它定义了一个新类、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许你一世情深 2024-11-25 03:54:14

更新

截至 roxygen2 > 3.0.0 该软件包在为您解决所有这些问题方面变得更加智能。您现在只需要 @export 标签,roxygen 就会计算出什么您正在记录的事情,并在转换期间编写 NAMESPACE 等时执行适当的操作。

在某些例外情况下,您可能需要帮助roxygen。 Hadley Wickham 在他的 R 包中使用的示例 书是all.equal.data.frame。该函数名称对于什么是类以及什么是通用函数(allall.equalall.equal.data< /代码>)?

在这种情况下,您可以通过显式告知 roxygen 泛型和类/方法来帮助它,例如,

@method all.equal data.frame

如果您需要显式使用 @method,下面的原始答案解释了有关旧行为的更多信息。


原始

函数应使用 @method 标签进行记录:

#' @method print surveyor

在初次阅读时,@hadley 的文档对我来说有点混乱,因为我不熟悉 roxygen,但经过几次阅读后阅读该部分的内容后,我想我理解您需要 @method 的原因。

您正在为 print 方法编写完整文档。 @S3methodNAMESPACE 相关,并安排要导出的方法@S3method 并不用于记录方法。

您的 Rd 文件的 usage 部分应包含以下内容:

\method{print}{surveyor}(x, ...)

如果工作正常,因为这是在 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 the NAMESPACE 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, or all.equal.data)?

In such cases, you can help roxygen out by explicitly informing it of the generic and class/method, e.g.

@method all.equal data.frame

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:

#' @method print surveyor

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 the NAMESPACE 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:

\method{print}{surveyor}(x, ...)

if this works correctly, as that is the correct way to document S3 methods in Rd files.

丘比特射中我 2024-11-25 03:54:14

就roxygen2而言, 3.0.0.,您只需要 @export 因为 roxygen 可以确定 print.surveyor 是一个 S3 方法。这意味着您现在只需要

#' Prints surveyor object.
#' 
#' @param x surveyor object
#' @param ... ignored
#' @export
print.surveyor <- function(x, ...){
    cat("Surveyor\n\n")
    print.listof(x)
}

然而,在这种情况下,由于文档不是很有用,最好只执行以下操作:

#' @export
print.surveyor <- function(x, ...){
    cat("Surveyor\n\n")
    print.listof(x)
}

As of roxygen2 > 3.0.0., you only need @export because roxygen can figure out that print.surveyor is an S3 method. This means that you now only need

#' Prints surveyor object.
#' 
#' @param x surveyor object
#' @param ... ignored
#' @export
print.surveyor <- function(x, ...){
    cat("Surveyor\n\n")
    print.listof(x)
}

However, in this case since the documentation isn't very useful, it'd probably better to just do:

#' @export
print.surveyor <- function(x, ...){
    cat("Surveyor\n\n")
    print.listof(x)
}
巴黎盛开的樱花 2024-11-25 03:54:14

@export 仅在加载泛型时才有效。如果泛型位于另一个包中,则需要导入泛型。 块来解决此问题

#' @importFrom tibble data_frame
#' @export
tibble::data_frame

在当前的 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 like

#' @importFrom tibble data_frame
#' @export
tibble::data_frame

taken 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 a reexports.Rd file that - needless to say - satisfies R CMD check.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文