R Help Page as Object

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));

Get help file description in R as an object

Seems to work for what you want but I'd take out the semi-colons:

R help page as object

Can be accessed by var$description.

In R, can I get the help text for a function into a variable?

I think help.search could be of use. For instance, if I wanted everything in the base package:

x <- help.search("*", package="base")
entries <- data.frame(entry=x$matches$Entry, title=x$matches$Title)
entries[c(1, 100, 1000),]
# entry title
# 1 + Arithmetic Operators
# 100 c.POSIXlt Date-Time Classes
# 1000 encoding Functions to Manipulate Connections

How to write contents of help to a file from within R?

Looks like the two functions you would need are tools:::Rd2txt and utils:::.getHelpFile. This prints the help file to the console, but you may need to fiddle with the arguments to get it to write to a file in the way you want.

For example:

hs <- help(survey)
tools:::Rd2txt(utils:::.getHelpFile(as.character(hs)))

Since these functions aren't currently exported, I would not recommend you rely on them for any production code. It would be better to use them as a guide to create your own stable implementation.

How to activate help() with a self created R package?

I use a <package-name>-package.R file which is populated with roxygen2 documentation and a single line of code: NULL.

#' Package contains functions used in daily work by our group for ecology at
#' Department of biology, Biotechnical faculty, University of Ljubljana.
#'
#' So far the functions included in the package are:
#' \itemize{
#' \item \code{\link{readClumpp}} Read result of Clumpp into a \code{data.frame}
#' \item \code{\link{writeStructure}} Write a \code{genind} object to be analyzed using Structure
#' \item \code{\link{writeGenePop}} Write a \code{genind} object to be analyzed using GENEPOP
#' \item \code{\link{drawLoci}} Plot alleles using a list of loci
#' }
#'
#' \tabular{ll}{
#' Package: \tab zvau\cr
#' Type: \tab Package\cr
#' Version: \tab 0.2\cr
#' Date: \tab 2015-02-11\cr
#' License: \tab GPL-2\cr
#' LazyData: \tab no\cr
#' }
#'
#' @author Roman Lustrik (\email{roman.lustrik@@emaildomain.com})(\email{mjelencic@@gmail.com})
#'
#' Maintainer: Roman Lustrik (\email{roman.lustrik@@emaildomain.com})
#' @name zvau-package
#' @import ggplot2
#' @aliases zvau
#' @docType package
#' @title Misc functions used by out group
#' @keywords package datasets
NULL

When I type ?zvau or help(zvau), I get the above documentation in R.

For 2), perhaps you are not exporting all "public" functions in your documentation?

how can I extract text from R's help command?

help itself doesn't return anything useful. To get the help text, you can read the contents of the help database for a package, and parse that.

extract_help <- function(pkg, fn = NULL, to = c("txt", "html", "latex", "ex"))
{
to <- match.arg(to)
rdbfile <- file.path(find.package(pkg), "help", pkg)
rdb <- tools:::fetchRdDB(rdbfile, key = fn)
convertor <- switch(to,
txt = tools::Rd2txt,
html = tools::Rd2HTML,
latex = tools::Rd2latex,
ex = tools::Rd2ex
)
f <- function(x) capture.output(convertor(x))
if(is.null(fn)) lapply(rdb, f) else f(rdb)
}

pkg is a character string giving the name of a package

fn is a character string giving the name of a function within that package. If it is left as NULL, then the help for all the functions in that package gets returned.

to converts the help file to txt, tml or whatever.

Example usage:

#Everything in utils
extract_help("utils")

#just one function
extract_help("utils", "browseURL")

#convert to html instead
extract_help("utils", "browseURL", "html")

#a non-base package
extract_help("plyr")


Related Topics



Leave a reply



Submit