Arguments of lm
function can be obtained by using:
args(lm)
Output
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
NULL
Questions
How to get:
lm (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
with the description (Not complete help) of each Argument to be used in Sweave
or knitr
. Thanks
Edited
Using funExtract function provided by @Ananda, I'm very close to my desired result. Here is code of my Rnw
file with output.
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Arguments for lm
<< label = funExtract, echo = TRUE, results = "hide", tidy = FALSE >>=
funExtract <- function(Function, section = "Usage") {
A <- deparse(substitute(Function))
x <- capture.output(tools:::Rd2txt(utils:::.getHelpFile(help(A))))
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)))]]
cat(out, sep = "\n")
invisible(out)
}
@
\vspace{0.5cm}\\
funExtract function output
\vspace{0.25cm}\\
<< label = lm-usage, echo = FALSE, results = "asis" >>=
funExtract(lm, section="Usage:")
@
\vspace{0.5cm}\\
args function output
\vspace{0.25cm}\\
<< label = lm-args, echo = FALSE, results = "asis" >>=
args(lm)
@
\end{document}
Output
Issues with funExtract function output
- How to get highlighted output from funExtract function as other code?
- How to remove section title form funExtract function output?
I have a function
usage()
in the formatR package that captures the arguments of a function. For now, you have to use the development version (>= 0.10.3).For knitr, I also have a recent change (i.e. please also test its development version on Github) so that you can the display function usage much more easily: you can use the new chunk option
code
to input code into a chunk.Put the two pieces together, you will be able to write a code chunk like this:
The reason that these feature came up recently was that I happened to need them by myself as well. I wanted to display the usage of functions with syntax highlighting. This solution is portable to all document formats that knitr supports, not limited to Rnw.
I had written a function and posted it as an answer earlier (as noted in the question itself), but wasn't entirely happy with the inconsistencies or the requirement that it had to be used with "markdown" to be used successfully. After a little bit more work, this is the function that I came up with:
Quite a mouthful, and it's not entirely DRY... but I wanted to capture four scenarios and this was the quickest idea that came to mind. The four scenarios I anticipate are:
type = "m_code"
)type = "m_text"
)type = "s_code"
)type = "s_text"
)The function extracts the output of
Rd2txt
. I picked that over the other formats (HTML, LaTeX) to allow me to use a single function to get what I was after and not have to create multiple functions.Usage
Usage is different depending on if you're creating a "Sweave" (.Rnw) or a "markdown" (.Rmd) document.
Markdown
Insert your code in a code chunk that looks something like this (maybe one day I'll add different methods, but not now):
Sweave
Pretend you are inserting a "child" document that should be included in the main document using
Sexpr{knit_child(.)}
I've created a Gist that includes the function, a sample Rmd file and an sample Rnw file. Feel free to leave comments and suggestions here on Stack Overflow (since Gist comments are pretty much meaningless since they don't notify the user when a comment is posted).
If you're trying to refer to a function from a package that is not currently loaded, the usage of
helpExtract
should be something like:You can use
Rd_db
to get the Rd data from a package.Extract the lm help from this:
Then use
capture.output
andRd2latex
to get latex of the help page:And pull out the segments you want to include in your rnw file: