首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新定义help_console函数以从给定的包获得函数的帮助

重新定义help_console函数以从给定的包获得函数的帮助
EN

Stack Overflow用户
提问于 2014-10-06 19:41:41
回答 1查看 95关注 0票数 0

下面是获得R函数帮助的函数。见下文:

代码语言:javascript
复制
help_console <-
function (topic, format = c("text", "html", "latex", "Rd"), lines = NULL,
    before = NULL, after = NULL)
{
    format = match.arg(format)
    if (!is.character(topic))
        topic <- deparse(substitute(topic))
    helpfile = utils:::.getHelpFile(help(topic))
    hs <- capture.output(switch(format, text = tools:::Rd2txt(helpfile),
        html = tools:::Rd2HTML(helpfile), latex = tools:::Rd2latex(helpfile),
        Rd = tools:::prepare_Rd(helpfile)))
    if (!is.null(lines))
        hs <- hs[lines]
    hs <- c(before, hs, after)
    cat(hs, sep = "\n")
    invisible(hs)
}

help_console(topic="lm", format = "text", lines=1)
Fitting Linear Models

现在,我想重新定义这个函数,以便从给定的包中获得关于R函数的帮助。这是我的MWE

代码语言:javascript
复制
help_console2 <-
function (topic, pkg, format = c("text", "html", "latex", "Rd"), lines = NULL,
    before = NULL, after = NULL)
{
    format = match.arg(format)
    if (!is.character(topic))
        topic <- deparse(substitute(topic))
    if (!is.character(pkg))
        topic <- deparse(substitute(pkg))
    helpfile = utils:::.getHelpFile(help(pkg, topic))
    hs <- capture.output(switch(format, text = tools:::Rd2txt(helpfile),
        html = tools:::Rd2HTML(helpfile), latex = tools:::Rd2latex(helpfile),
        Rd = tools:::prepare_Rd(helpfile)))
    if (!is.null(lines))
        hs <- hs[lines]
    hs <- c(before, hs, after)
    cat(hs, sep = "\n")
    invisible(hs)
}

help_console2(topic="lm", pkg="stats", format = "text", lines=1)

Error in find.package(if (is.null(package)) loadedNamespaces() else package,  : 
  there is no package called ‘topic’

这个函数是抛出错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-06 19:49:12

您有错误的论证顺序,需要比非标准的评估更明智:

代码语言:javascript
复制
help_console2 <-
  function (topic, pkg, format = c("text", "html", "latex", "Rd"), lines = NULL,
            before = NULL, after = NULL)
  {
    format = match.arg(format)
    if (!is.character(topic))
      topic <- deparse(substitute(topic))
    if (!is.character(pkg))
      topic <- deparse(substitute(pkg))
    helpfile = utils:::.getHelpFile(do.call(help, list(topic=topic, package=pkg)))
    hs <- capture.output(switch(format, text = tools:::Rd2txt(helpfile),
                                html = tools:::Rd2HTML(helpfile), latex = tools:::Rd2latex(helpfile),
                                Rd = tools:::prepare_Rd(helpfile)))
    if (!is.null(lines))
      hs <- hs[lines]
    hs <- c(before, hs, after)
    cat(hs, sep = "\n")
    invisible(hs)
  }

help_console2(topic="lm", pkg="stats", format = "text", lines=1)
#Fitting Linear Models
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26223630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档