如何使用roxygen记录包含具有相同名称的函数的R包?

bhe*_*ner 12 r roxygen roxygen2

我正在学习使用roxygen.我看到rd vignette提倡使用"_PACKAGE"来表示我正在创建包文档,并说"如果已经有一个名为pkgname()的函数,这也有效."

我也看过R包书的使用方法

NULL
Run Code Online (Sandbox Code Playgroud)

指定了@docType和@name,但是当我试图用任何一种方法制作一个玩具示例时,它并不像我期望的那样工作.

作为一个玩具示例,我想制作一个包含"hello()"函数的"hello"包.

我希望得到我的问候文件

?hello
Run Code Online (Sandbox Code Playgroud)

或者类似的东西

package?hello
Run Code Online (Sandbox Code Playgroud)

我希望得到有关所包含你好文档功能

?hello()
Run Code Online (Sandbox Code Playgroud)

我哪里错了? - 使用roxygen实现,我试图查询文档的方式,不正确的期望,还是其他什么?

我已经查看了有关包文档函数文档的问题,但对我来说仍然不清楚.

以下是我的玩具示例的一些细节:

你好/描述文件:

Package: hello
Type: Package
Title: A mostly empty package
Version: 0.1
Date: 2016-06-21
Authors@R: person("Some", "Person", email = "fake@madeup.org", role = c("aut", "cre"))
Description: More about what it does (maybe more than one line)
License: MIT
LazyData: TRUE
RoxygenNote: 5.0.1.9000
Run Code Online (Sandbox Code Playgroud)

你好/ R/hello.R

#' hello
#'
#' This is a mostly empty package to learn roxygen documentation.
#'
#' Hello allows me to learn how to write documentation in comment blocks
#' co-located with code.
#' @docType package
#' @name hello
"_PACKAGE"

#' hello
#'
#' This function returns "Hello, world!".
#' @export
#' @examples
#' hello()

hello <- function() {
  print("Hello, world!")
}
Run Code Online (Sandbox Code Playgroud)

有了这个,运行后document(),生成hello/man/hello.Rd.它包含我为hello包和hello()函数编写的描述的组合.?hello并且?hello()都返回.Rd文件.

这是.Rd的样子:

% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hello.R
\docType{package}
\name{hello}
\alias{hello}
\alias{hello-package}
\title{hello}
\usage{
hello()
}
\description{
This is a mostly empty package to learn roxygen documentation.

This function returns "Hello, world!".
}
\details{
Hello allows me to learn how to write documentation in comment blocks
co-located with code.
}
\examples{
hello()
}
Run Code Online (Sandbox Code Playgroud)

bhe*_*ner 8

通过@hadley,"不要使用@ name hello.这会覆盖默认命名"和"有时你需要重新启动R,因为devtools和dev docs有一些错误"

所以,如果我将hello.R文件更改为:

#' hello
#'
#' This is a mostly empty package to learn roxygen documentation.
#'
#' Hello allows me to learn how to write documentation in comment blocks
#' co-located with code.
"_PACKAGE"

#' hello
#'
#' This function returns "Hello, world!".
#' @export
#' @examples
#' hello()

hello <- function() {
  print("Hello, world!")
}
Run Code Online (Sandbox Code Playgroud)

然后document()制作hello-package.Rd和hello.Rd文件.我加载后library(hello),然后package?hello提供包文档,并?hello提供功能文档,因为我正在拍摄!

再次感谢你,@ hadley!

  • 现在看来这对我不起作用。我现在得到了 \alias{package} ,它搞乱了链接。 (2认同)
  • 我现在回来也遇到这个问题了。 (2认同)