Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use roxygen to document a R package that includes a function with the same name?

I'm learning to use roxygen. I see that the rd vignette advocates using "_PACKAGE" to indicate that I'm creating package documentation, and says "This also works if there’s already a function called pkgname()."

I've also seen the R packages book approach of using

NULL

with @docType and @name specified, but when I attempt to make a toy example with either approach, it doesn't work as I expect.

As a toy example, I'd like to make a "hello" package that includes a "hello()" function.

I expect to get documentation about my hello package with

?hello

or perhaps something like

package?hello

and I expect to get documentation about the included hello function with

?hello()

Where am I going wrong? - implementation with roxygen, the way I'm attempting to query the documentation, incorrect expectations, or something else?

I've already looked at questions about package documentation and function documentation, but things remain unclear to me.

Here's some details about my toy example:

hello/DESCRIPTION file:

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

hello/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!")
}

With this, after I run document(), hello/man/hello.Rd is generated. It contains a combination of the descriptions I've written for the hello package and the hello() function. ?hello and ?hello() both return that .Rd file.

Here's what the .Rd looks like:

% 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()
}
like image 878
bheavner Avatar asked Jun 21 '16 19:06

bheavner


People also ask

How do I write R package documents?

To add documentation to an R package, you need to create a subdirectory “ man ” containing a set of files, one per function, in a special R Documentation format ( . Rd ). These will be the source for the documentation for each function; R processes them to create plain text, PDF, and HTML versions.

How do I get the document function in R?

help operator in R provide access to the documentation pages for R functions, data sets, and other objects, both for packages in the standard R distribution and for contributed packages. To access documentation for the standard lm (linear model) function, for example, enter the command help(lm) or help("lm") , or ?

What is .RD file in R?

R objects are documented in files written in “R documentation” (Rd) format, a simple markup language much of which closely resembles (La)TeX, which can be processed into a variety of formats, including LaTeX, HTML and plain text.


1 Answers

via @hadley, "don’t use @ name hello. That overrides default naming" and "sometimes you need to restart R because there’s something buggy with devtools and dev docs"

So, if I change my hello.R file to this:

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

then document() makes hello-package.Rd and hello.Rd files. After I load library(hello), then package?hello provides package documentation, and ?hello provides function documentation, as I was shooting for!

Thank you once again, @hadley!

like image 73
bheavner Avatar answered Sep 28 '22 17:09

bheavner