R 包的 HTML 帮助页面中的图形(R 代码执行结果)

发布于 2024-10-12 12:44:11 字数 878 浏览 1 评论 0 原文

在 R 中编写包时,您可以创建 Rd 格式的帮助页面,然后将其转换为 HTML 页面。如果帮助页包含示例代码,则会在“示例”部分中打印。

例如,“stats”包的“prcomp”函数有两个页面:

  1. 仅示例代码: http://stat.ethz.ch/R-manual/R-patched/library/stats/html/prcomp.html
  2. 示例代码 + 带数字的结果: 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":

  1. Only example code: http://stat.ethz.ch/R-manual/R-patched/library/stats/html/prcomp.html
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美煞众生 2024-10-19 12:44:11

您可能想查看 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.

铜锣湾横着走 2024-10-19 12:44:11

感谢@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 包的人分享我的解决方案。

此致,
安德烈

#!/bin/bash

rdfile="fitSpline.Rd"
rdname=$(echo "$rdfile" | cut -d'.' -f1)

rfile=$rdname.R
sed -n '/\examples{/,/}/p' $rdfile > $rfile # text between two patterns
sed -i 's/\\examples{//' $rfile # remove pattern '\examples{'
sed -i 's/}$//' $rfile # remove the character '}'

rnwfile=$rdname.Rnw
cp $rfile $rnwfile
sed -i '1 i png("Rplot%03d.png")' $rnwfile
sed -i '1 i  <<example, echo=true, results=tex>>=' $rnwfile
sed -i '$ a dev.off()' $rnwfile
sed -i '$ a @' $rnwfile

texfile=$rdname.tex
R CMD Sweave $rnwfile
sed -i 's/\\begin{Schunk}//' $texfile
sed -i 's/\\begin{Sinput}//' $texfile
sed -i 's/\\end{Schunk}//' $texfile
sed -i 's/\\end{Sinput}//' $texfile
sed -i '/^$/d' $texfile # remove empty lines

reshtmlfile=$rdname.results.html
echo "<h3>Results</h3>" > $reshtmlfile
echo "<pre>" >> $reshtmlfile
cat $texfile >> $reshtmlfile
echo "</pre>" >> $reshtmlfile

for fig in $(ls *.png) ; do
  echo "<br><a href=\"$fig\"><img src=\"$fig\"></a>" >> $reshtmlfile
done

htmlfile=$rdname.html
R CMD Rdconv -t html $rdfile > $htmlfile

sed -i 's/<\/body>//' $htmlfile
sed -i 's/<\/html>//' $htmlfile
cat $reshtmlfile >> $htmlfile
echo "</body>" >> $htmlfile
echo "</html>" >> $htmlfile

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

#!/bin/bash

rdfile="fitSpline.Rd"
rdname=$(echo "$rdfile" | cut -d'.' -f1)

rfile=$rdname.R
sed -n '/\examples{/,/}/p' $rdfile > $rfile # text between two patterns
sed -i 's/\\examples{//' $rfile # remove pattern '\examples{'
sed -i 's/}$//' $rfile # remove the character '}'

rnwfile=$rdname.Rnw
cp $rfile $rnwfile
sed -i '1 i png("Rplot%03d.png")' $rnwfile
sed -i '1 i  <<example, echo=true, results=tex>>=' $rnwfile
sed -i '$ a dev.off()' $rnwfile
sed -i '$ a @' $rnwfile

texfile=$rdname.tex
R CMD Sweave $rnwfile
sed -i 's/\\begin{Schunk}//' $texfile
sed -i 's/\\begin{Sinput}//' $texfile
sed -i 's/\\end{Schunk}//' $texfile
sed -i 's/\\end{Sinput}//' $texfile
sed -i '/^$/d' $texfile # remove empty lines

reshtmlfile=$rdname.results.html
echo "<h3>Results</h3>" > $reshtmlfile
echo "<pre>" >> $reshtmlfile
cat $texfile >> $reshtmlfile
echo "</pre>" >> $reshtmlfile

for fig in $(ls *.png) ; do
  echo "<br><a href=\"$fig\"><img src=\"$fig\"></a>" >> $reshtmlfile
done

htmlfile=$rdname.html
R CMD Rdconv -t html $rdfile > $htmlfile

sed -i 's/<\/body>//' $htmlfile
sed -i 's/<\/html>//' $htmlfile
cat $reshtmlfile >> $htmlfile
echo "</body>" >> $htmlfile
echo "</html>" >> $htmlfile
软糖 2024-10-19 12:44:11

我想让你知道 R 2.14 中出现了一个新的解决方案 (参见此处)

Rd 标记有一个新的 \figure 标签,以便数字可以包含在
转换为 HTML 或 LaTeX 时的帮助页面。上面有例子
par() 和points() 的帮助页面。

Hadley,你的帮助库听起来很棒,一旦你把它的新版本放在 CRAN 上,我很乐意尝试一下。

I thought of letting you know that there is a new solution coming out in R 2.14 (see here):

Rd markup has a new \figure tag so that figures can be included in
help pages when converted to HTML or LaTeX. There are examples on the
help pages for par() and points().

Hadley, your helpr library sounds great, I would love to try it out once you put a newer version of it on CRAN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文