Last active
December 29, 2015 00:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`") | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modification to deal with non-existent sections?