caldwellst
caldwellst

Reputation: 5956

Function to return source documentation of dataset in package

Let's say I've developed a package. In my package I have a dataset called df, which is documented using roxygen2:

#' Title
#' 
#' Description
#' 
#' Details
#' 
...
...
#' @source La la la.
#' @source Fa fa fa.
...
...

This of course generates man/df.Rd, which has among other things the source section:

...
\source{
La la la.

Fa fa fa.
}
...

Now, I want to create a simple function with this dataset that returns the source of df. I can extract, process, and return the source from readLines(), however, I'm not sure how to reference filepaths here or how I would access man/df.Rd!

get_source <- function() {
  x <- readLines("path_to/df.Rd")
  x <- process(x)
  x
}
  1. How can I access man/df.Rd from within my package?
  2. Is there a simpler way to pull in the source, either from the roxygen2 documentation or generated .Rd file?

Upvotes: 1

Views: 142

Answers (2)

Waldi
Waldi

Reputation: 41220

You could use the following function to get the .Rd documentation :

getsource <- function(fn) {
  fn <- rlang::ensym(fn)
  utils:::.getHelpFile(help(deparse(fn)))
}

getsource(help)
#> \title{Documentation}\name{help}\alias{help}\keyword{documentation}\description{
#>   \code{help} is the primary interface to the help systems.
#> }\usage{
#> help(topic, package = NULL, lib.loc = NULL,
#>      verbose = getOption("verbose"),
#>      try.all.packages = getOption("help.try.all.packages"),
#>      help_type = getOption("help_type"))
#> }\arguments{
#>   \item{topic}{usually, a \link{name} or character string specifying the
#>    topic for which help is sought.  A character string (enclosed in
#>    explicit single or double quotes) is always taken as naming a topic.
#> 
...

Created on 2020-09-04 by the reprex package (v0.3.0)

This also works with source documentation of custom packages.

Upvotes: 1

caldwellst
caldwellst

Reputation: 5956

user2554330's comment led me to further explore opportunities using functions available. through tools:: . However, as they noted, this gets much more complex to parse. Thus, I explored different avenues and it's just much easier and simpler to reverse the approach. Define a function that outputs source and then use that within roxygen2 documentation and my desired function.

raw_source <- function(ret) {
  sources <- c("La la la.", "Fa fa fa.")
  sources[match(ret, c("a", "b"))]
}

get_source <- function() {
  x <- c(raw_source("a"), raw_source("b"))
  x <- process(x)
  x
}

#' Title
#' 
#' Description
#' 
#' Details
#' 
...
...
#' @source `r get_source("a")`
#' @source `r get_source("b")`
...
...

Doesn't work

Finally found out that system.file() can access specific directories within a package, had not been aware of it previously.

get_source <- function() {
  x_path <- system.file("man/df.Rd")
  x <- readLines(x_path)
  x <- process(x)
  x
}

As user2554330 pointed out, man directory isn't installed, so the above doesn't work.

Upvotes: 0

Related Questions