在 R 中编写包时,您可以创建 Rd 格式的帮助页面,然后将其转换为 HTML 页面。如果帮助页包含示例代码,则会在“示例”部分中打印。
例如,“stats”包的“prcomp”函数有两个页面:
- 仅示例代码: http://stat.ethz.ch/R-manual/R-patched/library/stats/html/prcomp.html
- 示例代码 + 带数字的结果:
http://rgm2.lab .nig.ac.jp/RGM2/R_man-2.9.0/library/stats/man/prcomp.html
问题是如何生成R代码执行的结果,特别是包括输出数字?这对应于“结果”部分。
我使用以下命令将 Rd 转换为 HTML:
R CMD Rdconv -t html $rdfile > $rdname.html
这会调用 R 函数 http://stat.ethz.ch/R-manual/R-devel/library/tools/html/Rd2HTML.html。
我将不胜感激任何意见或建议。
谢谢。
When writing a package in R, you can create the help pages in Rd format and then convert them into HTML pages. If the help page includes the example code, it is printed in Section "Examples".
For example, there are two pages for the function "prcomp" of the package "stats":
- Only example code: http://stat.ethz.ch/R-manual/R-patched/library/stats/html/prcomp.html
- Example Code + Results with Figures:
http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/stats/man/prcomp.html
The question is how to generate the results of R code execution, and particularly include the output figures? That corresponds to Section "Results".
I use the following command to convert Rd to HTML:
R CMD Rdconv -t html $rdfile > $rdname.html
That calls the R function http://stat.ethz.ch/R-manual/R-devel/library/tools/html/Rd2HTML.html.
I will appreciate any comments or suggestions.
Thanks.
发布评论
评论(3)
您可能想查看
helpr
包,它为文档提供了一个 Web 前端,以及许多其他改进,可以在线显示示例的结果。You might want to check out the
helpr
package which provides a web front end to the documentation that, as well as many other improvements, displays the results of examples in line.感谢@rcs 和@hadley 的评论。
实际上,这两种建议的解决方案似乎都不符合我的需求。以 Rd 格式嵌入图像则并非如此,因为我使用过渡 Roxygen>Rd。 “helpr”软件包确实令人印象深刻,但我认为它更适合构建计算机中安装的所有软件包的知识库。作为一名包开发人员,我需要一些更基本、更灵活的东西来自己进行更改。
最后,我得到了我所期望的, fitSpline.html 。这与我在问题中放入的参考页面非常相似,prcomp.html。
我发现没有办法采用“工具”包来在 HTML 文档中包含图像,至少目前是这样。因此,我编写了一个 bash 脚本,该脚本在输入上获取 Rd 文件,提取“\examples”部分并通过运行 Sweave 获取 html/image 输出。然后,“结果”部分的 html 部分与通过命令“R CMD Rdconv -t html”获得的 html 页面合并。
这似乎是很多代码,但我只想与那些也编写 R 包的人分享我的解决方案。
此致,
安德烈
Thanks @rcs and @hadley for your comments.
Actually , both proposed solutions don't seem to fit to my needs. Embedding images in Rd format is not the case, as I use transition Roxygen>Rd. The package 'helpr' is really impressive, but I think it suits more for building a knowledge base of all packages you have installed in your computer. I needed something more basic with flexibility to do changes by myself as a package developer.
Finally, I got what I expected, fitSpline.html. That is quite similar to the reference page I put in the question, prcomp.html.
I found that there is no way to adopt the package 'tools' to have images in HTML documentation, at least for now. Thus, I wrote a bash script that takes a Rd file on the input, extracts the section '\examples' and get html/image output by running Sweave. Afterwards, the html part of the 'Results' section is merged with the html page obtained by the command 'R CMD Rdconv -t html'.
That seems to be a lot of code, but I just want to share my solution with those who also writes R packages.
Best regards,
Andrey
我想让你知道 R 2.14 中出现了一个新的解决方案 (参见此处):
Hadley,你的帮助库听起来很棒,一旦你把它的新版本放在 CRAN 上,我很乐意尝试一下。
I thought of letting you know that there is a new solution coming out in R 2.14 (see here):
Hadley, your helpr library sounds great, I would love to try it out once you put a newer version of it on CRAN.