Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Last active December 29, 2015 00:29
Show Gist options
  • Save mrdwab/7586769 to your computer and use it in GitHub Desktop.
Save mrdwab/7586769 to your computer and use it in GitHub Desktop.
Extracts the requested section of a function's help files from R to the console.
helpExtract <- function(Function, section = "Usage", type = "m_code") {
A <- deparse(substitute(Function))
x <- capture.output(tools:::Rd2txt(utils:::.getHelpFile(utils::help(A)),
options = list(sectionIndent = 0)))
B <- grep("^_", x) ## section start lines
x <- gsub("_\b", "", x, fixed = TRUE) ## remove "_\b"
X <- rep(FALSE, length(x))
X[B] <- 1
out <- split(x, cumsum(X))
out <- out[[which(sapply(out, function(x)
grepl(section, x[1], fixed = TRUE)))]][-c(1, 2)]
while(TRUE) {
out <- out[-length(out)]
if (out[length(out)] != "") { break }
}
switch(
type,
m_code = {
before <- "```r"
after <- "```"
c(before, out, after)
},
s_code = {
before <- "<<>>="
after <- "@"
c(before, out, after)
},
m_text = {
paste(" ", out, collapse = "\n")
},
s_text = {
before <- "\\begin{verbatim}"
after <- "\\end{verbatim}"
c(before, out, after)
},
stop("`type` must be either `m_code`, `s_code`, `m_text`, or `s_text`")
)
}
R Function Help Pages
========================================================
```{r, echo=FALSE}
library(devtools)
source_gist("https://gist.github.com/mrdwab/7586769")
```
How do I insert this?
```{r echo=FALSE}
helpExtract(cor)
```
```{r, echo=FALSE, results='asis'}
cat(helpExtract(cor), sep = "\n")
```
```{r, echo=FALSE, results='asis'}
cat(helpExtract(cor, section = "Argu", type = "m_text"))
```
Here's another section:
```{r, echo=FALSE, results='asis'}
cat(helpExtract(readChar, section = "Deta", type = "m_text"))
```
\documentclass{article}
\begin{document}
<<echo=FALSE>>=
library(devtools)
source_gist("https://gist.github.com/mrdwab/7586769")
@
\Sexpr{knit_child(textConnection(helpExtract(cor, section="Arg", type = "s_text")),
options = list(tidy = FALSE, eval = FALSE))}
\Sexpr{knit_child(textConnection(helpExtract(cor, type = "s_code")),
options = list(tidy = FALSE, eval = FALSE))}
<<tidy=TRUE>>=
## normal R code
args(lm)
@
\end{document}
@mrdwab
Copy link
Author

mrdwab commented Oct 6, 2014

Modification to deal with non-existent sections?

helpExtract <- function(Function, section = "Usage", type = "m_code") {
  A <- deparse(substitute(Function))
  x <- capture.output(tools:::Rd2txt(utils:::.getHelpFile(utils::help(A)),
                                     options = list(sectionIndent = 0)))
  B <- grep("^_", x)                      ## section start lines
  x <- gsub("_\b", "", x, fixed = TRUE)   ## remove "_\b"
  X <- rep(FALSE, length(x))              ## Create a FALSE vector
  X[B] <- 1                               ## Initialize
  out <- split(x, cumsum(X))              ## Create a list of sections
  sectionID <- vapply(out, function(x)    ## Identify where the section starts
    grepl(section, x[1], fixed = TRUE), logical(1L))

  if (!any(sectionID)) {                  ## If the section is missing...
    ""                                    ## ... just return an empty character 
  } else {                                ## Else, get that list item
    out <- out[[which(sectionID)]][-c(1, 2)]
    while(TRUE) {                         ## Remove the extra empty lines
      out <- out[-length(out)]            ##   from the end of the file
      if (out[length(out)] != "") { break }
    } 

    switch(                               ## Determine the output type
      type,
      m_code = {
        before <- "```r"
        after <- "```"
        c(before, out, after)
      },
      s_code = {
        before <- "<<eval = FALSE>>="
        after <- "@"
        c(before, out, after)
      },
      m_text = {
        paste("    ", out, collapse = "\n")
      },
      s_text = {
        before <- "\\begin{verbatim}"
        after <- "\\end{verbatim}"
        c(before, out, after)
      },
      stop("`type` must be either `m_code`, `s_code`, `m_text`, or `s_text`")
    )
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment