以下是获取R功能帮助的功能。见下文:
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
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’
此函数抛出错误。任何帮助将受到高度赞赏。感谢
答案 0 :(得分:1)
您的参数顺序错误,需要超越非标准评估:
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