Mikael Jagan
Mikael Jagan

Reputation: 11326

Documenting a package that defines a function with the same name

I am writing a package named foo that defines an S3 class named foo with various S3 methods. I have written a constructor function foo() that returns a foo object. It seemed practical to name the class after the package, and the function after the class, and I hoped that:

But what happens is that:

Is there a way to give a package and a function the same name that produces my desired behaviour?

Currently I have a file foo_package.R like this:

#' The foo package
#' 
#' A very useful package.
#' 
#' @docType package
#' @name foo
NULL

and a file foo.R like this:

#' The foo function
#' 
#' A very useful function.
#' 
#' @param x A data frame.
#' @return A foo object.
#' @export
foo <- function(x) {
  structure(x, class = c("foo", "data.frame"))
}

Any hints are appreciated...

Upvotes: 2

Views: 255

Answers (1)

Mikael Jagan
Mikael Jagan

Reputation: 11326

Following the second link in @MrFlick's comment, which points to the text under "Packages" in vignette("rd"), I was able to get the expected behaviour.

foo.R is unchanged, but foo-package.R now reads:

#' The foo package
#' 
#' A very useful package.
#' 
#' @docType package
#' @keywords internal
#' @aliases foo-package
"_PACKAGE"

Now, as desired:

  • package?foo and ?"foo-package" bring up the package help.
  • ?foo and ?foo::foo bring up the function help.

Upvotes: 2

Related Questions