如何将R帮助文件的html或文本存储到变量中

时间:2015-06-02 04:28:00

标签: r

我想将R函数的helpfile存储到变量中。我更喜欢存储html版本。我想这样做,以便我可以将其写入本地.html文件。

例如,help(glm)在Rstudio的Help窗格中生成以下文字:

glm {stats} R Documentation
Fitting Generalized Linear Models
Description

glm is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor and a description of the error distribution.
Usage
...

和相应的带有markdown的html版本可在以下位置找到:https://stat.ethz.ch/R-manual/R-devel/library/stats/html/glm.html

但是,我还没有发现将此html的副本存储为变量。我尝试了helptext <- help(glm),它提供了

> str(helptext)
Class 'help_files_with_topic'  atomic [1:1] /usr/lib/R/library/stats/help/glm
  ..- attr(*, "call")= language help(topic = glm)
  ..- attr(*, "topic")= chr "glm"
  ..- attr(*, "tried_all_packages")= logi FALSE
  ..- attr(*, "type")= chr "html"

1 个答案:

答案 0 :(得分:2)

感谢@Akrun发布的链接,我意识到对现有答案的简单修改会生成一个html文件,方法是将Rd2txt更改为Rd2HTML

file <- help("glm")
pkgname <- basename(dirname(dirname(file)))
temp <- tools::Rd2HTML(utils:::.getHelpFile(file), out = tempfile("Rtxt"), 
                  package = pkgname)
file.copy(temp,"~/test.html")