链接到 R 中 roxygen2文档中的其他包

我想知道是否有一个方法可以从 link的其他软件包的功能,当我试图写一个新的软件包使用 roxygen2的文档。 \link{pck=PACKAGE_NAME, fun=FUNCTION_NAME}之类的?

17937 次浏览

You have to type \link[pkg]{function} e.g. \link[stringi]{stri_c}

In addition to the answer by potockan:
Some packages document several functions in a single help page. For example, the trim function from Bioconductor package GenomicRanges is documented in intra-range-methods (which is also the name of a help page from other packages such as IRanges).

To link to the right page with roxygen2 you can use:

\link[GenomicRanges:intra-range-methods]{trim}

or

\code{\link[GenomicRanges:intra-range-methods]{trim}}

to format the text correctly.

The help page will only show trim but will link to the right help page.

Roxygen2 now also supports documentation written in markdown.

The markdown syntax is for the link is [foo::bar()] which is translated to \code{\link[foo:bar]{foo::bar()}} in the generated .Rd file. (See Roxygen2 vignette.)

Note that you may need to specifically turn on Markdown support by writing Roxygen: list(markdown = TRUE) in your DESCRIPTION file, or by putting an #' @md comment if you want to enable markdown only for a specific man page. This is also explained at the very top of the linked vignette. (Thanks to @Tjebo for the comment)

Note that there are two colons in the markdown version whereas there is only one colon in the Rd version.

There are two ways to achieve this, using:

1. .Rd syntax

From "R packages" book:

  • \code{\link{function}} - function in this package.
  • \code{\link[MASS]{abbey}} - function in another package.
  • \link[=dest]{name} - link to dest, but show name.
  • \code{\link[MASS:abbey]{name}} - link to function in another package, but show name.
  • \linkS4class{abc} - link to an S4 class.

2. markdown syntax

From roxygen2 vignette

roxygen2 comment description generated Rd code
[func()] func() in this package \code{\link[=func]{func()}}
[pkg::func()] func() in the pkg \code{\link[pkg:func]{pkg::func()}}
[thing] topic in this package \link{thing}
[pkg::thing] topic in the pkg \link[pkg:thing]{pkg::thing}
[`thing`] topic in this package \code{\link{thing}}
[`pkg::thing`] topic in the pkg \code{\link[pkg:thing]{pkg::thing}}

Remember to put Roxygen: list(markdown = TRUE) in the DESCRIPTION
Markdown shortcodes available from roxygen2>=6.0.0

To link to a function, we slightly abuse markdown syntax by using [function()] or [pkg::function()].

  • Re-document your package by pressing Cmd/Ctrl + Shift + D.

  • Build and install your package by clicking in the build pane or by pressing Ctrl/Cmd + Shift + B. This installs it in your regular library, then restarts R and reloads your package.

  • Preview documentation with ?