R package documentation: link to a whole package, not function
You cannot link to the page that comes up when you click on the name of a package in RStudio's Packages pane. RStudio invisibly calls help(package = "<name>")
, which renders the package's index.
From the R-exts
manual:
The markup
\link{foo}
(usually in the combination\code{\link{foo}}
) produces a hyperlink to the help for foo. Here foo is a topic, that is the argument of\alias
markup in anotherRd
file (possibly in another package).
Hence \link
and the equivalent Markdown markup supported by roxygen2
can only link to topics. A package index is not a topic in this sense because there is no corresponding Rd
file.
It might be best just to remind users that they can use help
to access the index of the package to which you are referring.
R object documentation: Package documentation as first item
In order to ensure that the package documentation entry is first in the manual, you need its name
to be <packagename>-package
(where you replace <packagename>
with the name of your package). Here you could accomplish that by changing
#' @name TestPackage
to
#' @name TestPackage-package
Why is this the case? Well, as always when you run into some strange corner of R package development, the best thing you can do is to consult the Writing R Extensions manual. Here, we'd want to look at Section 2.1, Rd format (the documentation files in R are in a format called Rd
; roxygen
creates the Rd
files in man/
for you). Specifically, in Section 2.1.1, we see
\name{name}
name typically101 is the basename of the Rd file containing the documentation. ... Entries in the package manual will be in alphabetic102 order of the \name entries.
That footnote 102 tells us
in the current locale, and with special treatment for LaTeX special characters and with any ‘pkgname-package’ topic moved to the top of the list.
So, you have to name
the Rd
file as pkgname-package
to move it to the top of the manual.
Where are objects stored in R packages?
Variables can also be created in a normal R script of the package with foo <- new.env()
and then populated with content, e.g.
foo <- new.env()
foo <- data.frame(
x=1:10,
y <- rnorm(10)
)
It is also possible to make the internal data hidden, e.g. .foo
. As an example CRAN package marelac uses this mechanism in files aaa.R
, gas_schmidt.R
and some others.
Another possibility is a file sysdata.rda
that can be placed in the /R
folder, see: https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-subdirectories
Non-file package-anchored link(s) in documentation object
It seems that, if you use links of the form \code{\link[pkg]{foo}}
pointing to help pages containing several functions, the link should point only to the core function.
For example, say the functions foo_1
and foo_2
are described in the same help page and foo_1
is the main function (i.e. appearing in the top left corner of the help page).
To create a link to foo_2
, you should use \code{\link[pkg:foo_1]{foo_2}}
.
See this GH issue and this reference in writing R extensions.
R won't find objects
So all I needed to do was install the package instead of using the .R and .Rd files on my working directory.
R help page as object
So below what I hacked together. However I yet have to test it on many help files to see if it generally works.
Rd2list <- function(Rd){
names(Rd) <- substring(sapply(Rd, attr, "Rd_tag"),2);
temp_args <- Rd$arguments;
Rd$arguments <- NULL;
myrd <- lapply(Rd, unlist);
myrd <- lapply(myrd, paste, collapse="");
temp_args <- temp_args[sapply(temp_args , attr, "Rd_tag") == "\\item"];
temp_args <- lapply(temp_args, lapply, paste, collapse="");
temp_args <- lapply(temp_args, "names<-", c("arg", "description"));
myrd$arguments <- temp_args;
return(myrd);
}
getHelpList <- function(...){
thefile <- help(...)
myrd <- utils:::.getHelpFile(thefile);
Rd2list(myrd);
}
And then you would do something like:
myhelp <- getHelpList("qplot", package="ggplot2");
cat(jsonlite::toJSON(myhelp));
Related Topics
Change the Position of the Strip Label in Ggplot from the Top to the Bottom
Why Has Data.Table Defined := Rather Than Overloading <-
Error: Vector Memory Exhausted (Limit Reached) R 3.5.0 MACos
R: Merge Two Irregular Time Series
Raw Text Strings for File Paths in R
Removing Specific Rows from a Dataframe
Export Data Frames to Excel via Xlsx with Conditional Formatting
R Command Line Passing a Filename to Script in Arguments (Windows)
Tooltip When You Mouseover a Ggplot on Shiny
How to Insert a Dataframe into a SQL Server Table
Horizontal Dendrogram in R with Labels
How to Get Value When a Variable Name Is Passed as a String
Modify Glm Function to Adopt User-Specified Link Function in R