提取要在knitr中使用的R函数的参数 [英] Extracting arguments of an R function to use in knitr

查看:92
本文介绍了提取要在knitr中使用的R函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lm函数的参数可以通过以下方式获得:

Arguments of lm function can be obtained by using:

args(lm)

输出

function (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...) 
NULL

问题

如何获取:

lm (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...) 

,其中包含要在Sweaveknitr中使用的每个参数的描述(未完全帮助).谢谢

with the description (Not complete help) of each Argument to be used in Sweave or knitr. Thanks

已编辑

使用@Ananda提供的funExtract函数,我非常接近我想要的结果.这是带输出的Rnw文件的代码.

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}

输出

问题与funExtract函数输出有关

  1. 如何从funExtract函数获取突出显示的输出作为其他代码?
  2. 如何从funExtract函数输出中删除节标题?

推荐答案

我已经编写了一个函数并将其发布为较早的答案(如问题本身所示),但对不一致之处或要求并不完全满意它必须与"markdown"一起使用才能成功使用.经过更多的工作之后,这就是我想到的功能:

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:

helpExtract <- function(Function, section = "Usage", type = "m_code", ...) {
  A <- deparse(substitute(Function))
  x <- capture.output(tools:::Rd2txt(utils:::.getHelpFile(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 = c("```r", out, "```"),
    s_code = c("<<>>=", out, "@"),
    m_text = paste("    ", out, collapse = "\n"),
    s_text = c("\\begin{verbatim}", out, "\\end{verbatim}"),
    stop("`type` must be either `m_code`, `s_code`, `m_text`, or `s_text`")
  )
}

很宽口,而且不完全是DRY ...但是我想捕捉四个场景,这是我想到的最快的想法.我预期的四种情况是:

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:

  1. 文档类型为标记,用户正在提取代码块(type = "m_code")
  2. 文档类型为标记,用户正在提取非代码段(type = "m_text")
  3. 文档类型为Sweave,并且用户正在提取代码块(type = "s_code")
  4. 文档类型为Sweave,并且用户正在提取非代码段(type = "s_text")
  1. Document type is markdown and user is extracting a code block (type = "m_code")
  2. Document type is markdown and user is extracting a non-code section (type = "m_text")
  3. Document type is Sweave and user is extracting a code block (type = "s_code")
  4. Document type is Sweave and user is extracting a non-code section (type = "s_text")

该函数提取Rd2txt的输出.我选择了其他格式(HTML,LaTeX),使我可以使用单个函数来获取所需的内容,而不必创建多个函数.

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.

用法是不同的,具体取决于您要创建的是"Sweave"(.Rnw)还是"markdown"(.Rmd)文档.

Usage is different depending on if you're creating a "Sweave" (.Rnw) or a "markdown" (.Rmd) document.

  1. 降价

将您的代码插入看起来像这样的代码块中(也许有一天我会添加其他方法,但现在不行了):

Insert your code in a code chunk that looks something like this (maybe one day I'll add different methods, but not now):

```{r, echo=FALSE, results='asis'}
cat(helpExtract(cor), sep = "\n")
```

  • 调整

    假设您要插入一个子"文档,该文档应使用Sexpr{knit_child(.)}

    Pretend you are inserting a "child" document that should be included in the main document using Sexpr{knit_child(.)}

    \Sexpr{knit_child(textConnection(helpExtract(cor, type = "s_code")), 
    options = list(tidy = FALSE, eval = FALSE))}
    


  • 创建了包含以下功能的Gist ,一个示例Rmd文件和一个示例Rnw文件.随意在堆栈溢出的此处此处发表评论和建议(因为Gist评论几乎没有意义,因为它们不会在发布评论时通知用户).


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

    如果您试图从当前未加载的包中引用函数,则helpExtract的用法应类似于:

    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:

    helpExtract(gls, package = "nlme")
    

    这篇关于提取要在knitr中使用的R函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆