如何将 R 文本&图像输出捕获到一个文件(html、doc、pdf 等)中?
任务是创建一个文件(word、rtf、pdf、html 或其他),该文件将捕获 R 的输出(例如:不是创建输出的代码),转换为该格式(包括文本和图像)。
执行此操作的方法应该对原始 R 脚本进行尽可能少的更改。
如果我只关心文本或图像,那么我会使用 ?sink 或 ?pdf。但我不知道如何以一种简单的方式将两者合并为一个输出。
我知道有一种方法 使用 r2wd 导出 R 输出,但根据我的口味,它在原始代码中涉及太多奖章(我想 sweave 解决方案也是如此,尽管我没有使用它的经验来告诉)
这里是未来示例的示例代码:
START.text.and.image.recording("output.file") # this is the function I am looking for
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
plot(x,y)
print(summary(lm(y~x)))
STOP.text.and.image.recording("output.file") # this is the function I am looking for
更新:我被问到的方式不是 Sweave,或来自 ReproducibleResearch 任务视图。
原因是:
- 我(还)不知道 LaTeX
- 即使知道 LaTeX,我也想要一些具有简单默认值的东西来简单将所有输出按顺序转储在一起。 “简单”意味着 - 尽可能少的额外代码/文件管理开销。
我知道像 sweave 或 brew 这样的东西更具可扩展性,但我想看看是否有针对较小项目/脚本的更“简单”的解决方案。
The task is to create a file (word, rtf, pdf, html, or whatever) that will capture the output of R (e.g: not the code that created the output), into that format (including text and images).
The way of doing this should involve as little change to the original R script as possible.
If I had cared only for the text or images, then I would use ?sink, or ?pdf. But I don't know how to combine the two into one output in an easy way.
I know there is a way to export R output using r2wd, but it involves too much medaling in the original code for my taste (I imagine the same is true for the sweave solution, although I don't have experience with it to tell)
Here is a sample code for future examples:
START.text.and.image.recording("output.file") # this is the function I am looking for
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
plot(x,y)
print(summary(lm(y~x)))
STOP.text.and.image.recording("output.file") # this is the function I am looking for
Update: I was asked way not Sweave, or other options from ReproducibleResearch task view.
The reasons are:
- I don't (yet) know LaTeX
- Even knowing LaTeX, I want something with simple defaults to simply dump all the outputs together, and in order. "simply" means - as little extra code/file management overhead as possible.
I understand that something like sweave or brew are more scalable, but I am looking to see if there is a more "simple" solution for smaller projects/scripts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
截至 2012 年,
knitr
为这个问题提供了完美的解决方案。例如,创建一个扩展名为 rmd 的文件。将代码包装在几个命令中,如下所示:
您可以通过多种方式将其转换为独立的 HTML 文件。在 RStudio 中,您只需按一个按钮
Knit HTML
。这是生成的HTML文件;要实际查看 HTML 在浏览器中的显示方式,请保存文件并打开它。
图像代码和输出如您所料地交织在一起。
当然,您可以并且通常会将文件划分为多个 R 代码块。但重点是,你不必这样做。
以下是我创建的另外几个示例:
As of 2012
knitr
provides a perfect solution to this problem.For example, create a file with an rmd extension. Wrap your code in a couple of commands as follows:
You can convert it into a self-contained HTML file in several ways. In RStudio you just press a single button
Knit HTML
.This is the HTML file produced; to actually view how the HTML displays in a browser, save the file and open it.
Images code, and output are interweaved as you might expect.
Of course, you can and typically would divide up your file into multiple R code chunks. But the point is, you don't have to.
Here are another couple of examples I've created:
如果您了解 LaTeX,sweave 可能是您最好的选择。 odfWeave 是一种类似的机制,但用于将代码嵌入 OpenOffice.org 文件中。对于 HTML,有 R2html 包。但所有这些都可能需要您稍微分解代码才能充分利用系统。或者,您的 sweave/odfweave/html 模板可以在单个代码块中获取脚本的数据生成方面,并在需要的地方放置输出显示(
print()
语句)。您的图形也可以在脚本中调用,以生成要作为单独文件嵌入到文档中的图形,然后您可以手动将其包含在模板中。例如(这不是用于运行
sweave
的完整.Rnw
文件)在sweave文件中,您可以放置如下内容在模板的上方,它提供了 R 脚本的主要部分,该部分将进行分析并生成 R 对象:然后您将需要在想要打印输出的位置插入代码块:
然后您将需要块来插入数字。
将分析代码与输出(打印和/或图形)分开可能也是一种很好的做法,特别是如果分析代码在计算方面非常昂贵。您可以运行它一次 - 甚至缓存它 - 同时根据需要更新输出/显示代码。
示例 Sweave 文件
使用 csgillespie 的示例 sweave 文件,我将进行如下设置。首先是包含核心分析代码的
my_script.R
文件:然后是 Sweave 文件,
您似乎想要的东西并不存在;将 R 代码和输出组合到文档文件中的简单方法。如果您不认为 sweave 及其同类简单的话。您可能需要重新考虑您想要做什么或如何安排分析、图形和输出代码,但您可能最好查看建议的选项之一(sweave、odfweave、brew、R2html)。
华泰
If you know LaTeX, sweave will likely be your best bet. odfWeave is a similar mechanism but for embedding the code in an OpenOffice.org file. For HTML there is the R2html package. But all will likely require you to break the code up a little bit to get the best out of the systems. Alternatively, your sweave/odfweave/html template could source the data generation aspects of the script in a single code chunk, with the output display (
print()
statements) placed where required. Your graphics could also be called within the script to produce the figures to embed in the document as separate files, which you then include by hand in the template.For example (and this isn't a full
.Rnw
file for running throughsweave
) in a sweave file you'd put something like this high up in the template which sources the main part of the R script that will do the analysis and generate the R objects:Then you will need to insert code chunks where you want printed output:
Then you will need chunks to insert the figures.
Separating your analysis code from the output (printed and/or figures) is probably good practice as well, especially if the analysis code is expensive in compute terms. You can run it once - or even cache it - whilst updating the output/display code as you need to.
Example Sweave File
Using csgillespie's example sweave file I would set things up like this. First the
my_script.R
file containing the core analysis code:Then the Sweave file
What you seem to be wanting doesn't exist; a simple way of combining R code and output into a document file. That is if you don't consider sweave and its ilk simple. You might need to rethink what you want to do or how you arrange your analysis and graphics and output code, but you are likely best served looking at one of the suggested options (sweave, odfweave, brew, R2html).
HTH
我鼓励您使用 Sweave,但是使用
sink()
可以实现不太漂亮的基本功能。一个普通的txt文件:
或者添加一些HTML标签:
I would encourage you to use Sweave, but a rudimentary functionality that is not pretty can be achieved with
sink()
.A regular txt file:
or add some HTML tags:
大约一年前,我编写了一个名为 Roux 的脚本来执行此操作。我希望能够通过运行 R 脚本来创建 HTML 转录本(包括任何图像),而无需更改脚本。
您可以从命令行调用 Roux,如下所示:
roux example.R
和 roux 将:
Roux R 包是一个非常小的 R 包,它修改了plot()以及其他一些功能可以自动写入随机文件名而不是默认的交互式图形设备。
我已经用过很多次了,它对我来说非常有效,尽管我确信如果更多的人将它与新包一起使用,那么就会出现一些小问题,很可能你会有一个不同的函数来生成图表和 Roux不知道它应该为你打开一个 PNG 设备。
自从与 Tal 讨论这个问题以来,我更新并改进了代码,现在在这里:
http://bitbucket.org/ananelson/roux/src
因此,如果您遇到任何问题,请将它们报告给 Bitbucket 上的问题跟踪器。
我添加了对 LaTeX 转录本的支持,以便您可以轻松创建包含 R 脚本转录本(包括图像)的 PDF。 (如果您查看 example-output 目录,找到“raw”链接来下载它,您就可以看到一个示例。)
您确实需要安装 Python 和 Pygments python 库。如果您使用旧版本的 Python 并遇到任何问题,请告诉我。
我在博客上写过有关 Roux 的文章,但没有过多宣传,因为我的努力主要集中在一个名为 Dexy 的新项目上,该项目旨在替代 Sweave。如果您想要更多的灵活性和控制力,或者对文字文档感兴趣,那么您可能也想看看 Dexy。
I wrote a script called Roux about a year ago which does this. I wanted to be able to create HTML transcripts from running an R script, including any images, without having to change the script.
You call Roux from the command line, like this:
roux example.R
and roux will:
the Roux R package is a very small R package which modifies plot() and some other functions to automatically write to a random filename rather than the default interactive graphics device.
I have used this a lot, and it works really well for me, although I'm sure if more people use it with new packages then minor issues will arise, most likely that you'll have a different function which generates a graph and Roux won't know that it should open a PNG device for you.
Since speaking with Tal about this I have updated and improved the code, and it's now up here:
http://bitbucket.org/ananelson/roux/src
so if you run into any issues, please report them to the issue tracker there on Bitbucket.
I have added support for LaTeX transcripts so you can easily create PDFs which have the transcript of your R script including images. (You can see an example if you look in the example-output directory, find the "raw" link to download it.)
You do need to have Python and the Pygments python library intalled. If you have an older version of Python and run into any issues, please let me know.
I wrote about Roux on my blog but didn't publicize it that much because my efforts have been focused on a new project called Dexy which is intended as a replacement for Sweave. If you want more flexibility and control or are interested in literate documentation then you might want to check out Dexy too.
您在问题中提到了 sweave 但并没有真正说明为什么它不合适。你的问题似乎很适合 Sweave。事实上,您的示例代码可能来自第二个 Sweave 示例。
Sweave 文件示例
如果您了解 Latex,那么 Sweave 并不那么困难。以下是作为 Sweave 文件的示例文件:
在 Linux 下,只需将文件另存为 tmp.Rnw。然后
You mentioned sweave in your question but not really why it isn't suitable. Your question seems perfect for Sweave. In fact, your example code could have came from the second Sweave example.
Example Sweave file
If you know Latex then Sweave isn't that difficult. Here's your example file as a Sweave file:
Under linux, just save the file as tmp.Rnw. Then
还有 LyX,它具有 Sweave 界面。 R / LyX / Sweave 接口代码位于 CRAN http://cran.fhcrc.org/ contrib/extra/lyx/。 LyX 本身存在于大多数 Linux 发行版中。所有这些魔法都可以在 Windows 上运行,但这绝对不是小事。在 Windows 上,我建议使用 Blue Reference 中的 Inference for R 来进行 R 编程。
There is also LyX, which has an Sweave interface. The R / LyX / Sweave interface code is on CRAN at http://cran.fhcrc.org/contrib/extra/lyx/. LyX itself is in most of the Linux distros. All of this magic can be made to work on Windows, but it's definitely non-trivial. On Windows, I'd recommend Inference for R from Blue Reference for literate R progamming.
好吧,我只是提醒一下,我正在使用 Asciidoc 进行简短报告或编辑网页。现在有一个 R 插件 (ascii on CRAN),它允许将 R 代码嵌入到 asciidoc 文档中。其语法与 Markdown 或 Textile 非常相似,因此您会很快学会它。
通过最后两个后端之一输出为 (X)HTML、Docbook、LaTeX,当然还有 PDF。
不幸的是,我认为您无法将所有代码包装到单个语句中。然而,它支持大量的 R 对象,见下文。
Well, I just remind that I was using Asciidoc for short reporting or editing webpage. Now there's an R plugin (ascii on CRAN), which allows to embed R code into an asciidoc document. The syntax is quite similar to Markdown or Textile, so you'll learn it very fast.
Output are (X)HTML, Docbook, LaTeX, and of course PDF through one of the last two backends.
Unfortunately, I don't think you can wrap all your code into a single statement. However, it supports a large number of R objects, see below.
这是根据罗穆诺夫的回答,但仍然如此。您可以编写自己的打印,以某种 HTML 格式包装输出并将输出嵌入到 HTML 文件中。对于具有 数据 URI 方案 的图片也可以完成相同的操作,例如使用
img< /code> 来自
base64
R 包的函数。This is in light of romunov's answer, but still. You can just write your own print that wraps the output in some HTML formatting and embeds the output to a HTML file. The same can be done with pictures with Data URI scheme, for instance by using
img
function frombase64
R package.您可以使用
R2HTML
包将会话输出为html,并且TeachingDemos
包中有一些类似的函数(请参阅txtStart
)用于输出到增强文本和单词(通过R2wd
)。非图形命令将自动包含在文件中,并且可以通过单个命令插入当前绘图。You can use the
R2HTML
package to output a session to html and there are some similar functions in theTeachingDemos
package (seetxtStart
) for output to enhanced text and word (viaR2wd
). Non-graphics commands will be included in the file automatically and the current plot can be inserted by a single command.通过 Twitter 的奇迹,有人向我发送了一个指向此页面,关于一个名为“roux”的包。它是一年前创建的,我从未听说过它(显然你们大多数人也没有听说过)。
尽管安装看起来并不简单,但这个包似乎完全符合我在问题中寻找的效果。
我希望尝试一下这个解决方案,并看看其他 R 成员是否可以加入这个项目来更好地增强 R。
Through the wonders of twitter, someone reached out and sent me a link to this page, regarding a package called "roux". It was created a year ago, and I have never heard about it (apparently neither have most of you).
This package seems to do exactly what I was looking for in my question, although the installation seems non trivial.
I hope to play with this solution and also to see if other R members might go into this project to better enhance R.
@znmeb 的好建议是尝试 Lyx - 一个更像单词的 LaTeX 前端,并且正如文档所指出的,在 本版 R 新闻
这就是我在 Ubuntu 10.04 中按照 lyx sweave 存储库:
有用链接:
good suggestion by @znmeb to try Lyx - a more word-like front end for LaTeX, and as the documentation points out, there is a good article of its use with Sweave on page 2 of this edition of R news
This is how I did it in Ubuntu 10.04 follwoing the guidelines in the lyx sweave repository:
useful links: