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).
任务是创建一个文件(word,rtf,pdf,html或其他),它将R的输出(例如:不是创建输出的代码)捕获到该格式(包括文本和图像)。
The way of doing this should involve as little change to the original R script as possible.
这样做的方式应该是尽可能少地改变原始的R脚本。
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.
如果我只关心文本或图像,那么我会使用?sink,或?pdf。但我不知道如何以简单的方式将两者合并为一个输出。
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)
我知道有一种方法可以使用r2wd导出R输出,但是根据我的口味它在原始代码中涉及太多的介绍(我认为对于sweave解决方案也是如此,尽管我没有经验告诉它)
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.
更新:我被问到不是Sweave,或ReproducibleResearch任务视图中的其他选项。
The reasons are:
原因是:
- I don't (yet) know LaTeX
- 我(还)不知道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.
- 即使知道LaTeX,我想要一些简单的默认设置,只需将所有输出一起转储,然后按顺序排列。 “简单”意味着 - 尽可能少的额外代码/文件管理开销。
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.
我知道像sweave或brew这样的东西更具可扩展性,但我希望看看是否有一个更简单的解决方案适用于较小的项目/脚本。
11 个解决方案
#1
13
As of 2012 knitr
provides a perfect solution to this problem.
截至2012年,knitr为这一问题提供了完美的解决方案。
For example, create a file with an rmd extension. Wrap your code in a couple of commands as follows:
例如,创建一个扩展名为rmd的文件。将代码包装在几个命令中,如下所示:
```{r}
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)))
```
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.
您可以通过多种方式将其转换为自包含的HTML文件。在RStudio中,您只需按一个按钮编织HTML。这是生成的HTML文件;实际查看HTML在浏览器中的显示方式,保存文件并打开它。
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.
当然,您可以并且通常会将文件分成多个R代码块。但重点是,你不必。
Here are another couple of examples I've created:
这是我创建的另外几个例子:
- Getting started with R Markdown
- R Markdown入门
- Case study in using R Markdown
- 使用R Markdown的案例研究
#2
4
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.
如果你认识LaTeX,那么sweave可能是你最好的选择。 odfWeave是一种类似的机制,但用于将代码嵌入OpenOffice.org文件中。对于HTML,有R2html包。但是所有这些都可能需要您稍微破解代码以充分利用系统。或者,您的sweave / odfweave / html模板可以在单个代码块中获取脚本的数据生成方面,并在需要的位置放置输出显示(print()语句)。您也可以在脚本中调用图形,以生成要作为单独文件嵌入文档的图形,然后将其手动包含在模板中。
For example (and this isn't a full .Rnw
file for running through sweave
) 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:
例如(并且这不是一个完整的.Rnw文件,用于通过sweave运行)你可以在模板中放置这样的东西,它可以提供R脚本的主要部分来进行分析并生成R对象:
<<run_script, eval=TRUE, echo=FALSE, results=hide>>=
source("my_script.R")
@
Then you will need to insert code chunks where you want printed output:
然后,您需要在要打印输出的位置插入代码块:
<<disp_output, eval=TRUE, echo=FALSE, results=verbatim>>=
## The results=verbatim is redundant as it is the default, as is eval=TRUE
print(summary(x)) ## etc
@
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:
使用csgillespie的示例sweave文件,我会设置这样的东西。首先是包含核心分析代码的my_script.R文件:
x <- rnorm(100)
y <- jitter(x)
corXY <- cor(x,y)
mod.lm <- lm(y~x)
Then the Sweave file
然后是Sweave文件
\documentclass[12pt]{article}
\usepackage{Sweave}
\begin{document}
An introduction
<<run_analysis, eval=TRUE,echo=FALSE, results=hide>>=
source("my_script.R")
@
% Later
Here are the results of the analysis
<<show_printed_output, echo=FALSE>>=
summary(x))
head(data.frame(x,y))
@
The correlation between \texttt{x} and \texttt{y} is:
<<print_cor, echo=FALSE>>=
corXY
@
Now a plot
\begin{figure}[h]
\centering
<<echo=FALSE, eval=TRUE, fig=TRUE, width=6, height=4>>=
plot(x,y)
@
\caption{\textit{A nice plot.}}
\end{figure}
\end{document}
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).
你似乎想要的东西不存在;将R代码和输出组合成文档文件的简单方法。那就是如果你不认为sweave和它的同类简单。您可能需要重新考虑您想要做什么或如何安排分析,图形和输出代码,但您可能最好看一下建议的选项之一(sweave,odfweave,brew,R2html)。
HTH
HTH
#3
4
I would encourage you to use Sweave, but a rudimentary functionality that is not pretty can be achieved with sink()
.
我鼓励你使用Sweave,但是使用sink()可以实现一个不太漂亮的基本功能。
A regular txt file:
常规的txt文件:
sink(file = "test.txt", type = "output")
summary(cars)
sink()
or add some HTML tags:
或者添加一些HTML标签:
sink(file = "tal_test.html", type = "output")
cat("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"", "\n")
cat("\"http://www.w3.org/TR/html4/strict.dtd\">", "\n")
cat("<HTML>", "\n")
cat("<HEAD>", "\n")
cat("<TITLE>My first HTML document</TITLE>", "\n")
cat("</HEAD>", "\n")
cat("<BODY>", "\n")
summary(cars)
cat("</BODY>", "\n")
cat("</HTML>", "\n")
sink()
#4
3
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.
大约一年前,我写了一个名为Roux的脚本。我希望能够通过运行R脚本(包括任何图像)来创建HTML脚本,而无需更改脚本。
You call Roux from the command line, like this: roux example.R
您可以从命令行调用Roux,如下所示:roux example.R
and roux will:
和roux将:
- run the script in R (requiring the Roux package first automatically)
- 在R中运行脚本(首先需要Roux包)
- syntax highlight the .Rout output using Pygments
- 语法使用Pygments突出显示.Rout输出
- insert images in the correct location
- 将图像插入正确的位置
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.
Roux R包是一个非常小的R包,它修改了plot()和一些其他函数来自动写入随机文件名而不是默认的交互式图形设备。
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.
我已经使用了很多,它对我来说非常好用,虽然我确定如果有更多的人将它用于新的包,那么会出现小问题,很可能你会有一个不同的函数来生成图形和Roux我不知道它应该为你打开一个PNG设备。
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
自从与Tal谈论此事后,我已经更新并改进了代码,现在它已经出现了:http://bitbucket.org/ananelson/roux/src
so if you run into any issues, please report them to the issue tracker there on Bitbucket.
因此,如果您遇到任何问题,请将它们报告给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.)
我已经添加了对LaTeX脚本的支持,因此您可以轻松创建包含R脚本(包括图像)的PDF的PDF。 (如果你查看example-output目录,你可以看到一个例子,找到下载它的“原始”链接。)
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.
你需要安装Python和pygments python库。如果你有旧版本的Python并遇到任何问题,请告诉我。
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.
我在博客上写过关于Roux的文章,但没有那么多宣传,因为我的工作主要集中在一个名为Dexy的新项目上,该项目旨在替代Sweave。如果您想要更多的灵活性和控制,或者对文字文档感兴趣,那么您可能也想查看Dexy。
#5
2
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.
你在问题中提到了sweave但不是真的为什么它不合适。你的问题对Sweave来说似乎很完美。实际上,您的示例代码可能来自第二个Sweave示例。
Example Sweave file
示例Sweave文件
If you know Latex then Sweave isn't that difficult. Here's your example file as a Sweave file:
如果你知道Latex,那么Sweave就不那么难了。这是你的示例文件作为Sweave文件:
\documentclass[12pt,BCOR3mm,DIV16]{scrreprt}
\usepackage{Sweave}
\begin{document}
An introduction
<<eval=TRUE,echo=TRUE>>=
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
@
Now a plot
\setkeys{Gin}{width=0.5\textwidth}
\begin{figure}[h]
\centering
<<echo=FALSE, eval=TRUE, fig=TRUE, width=6, height=4>>=
plot(x,y)
@
\caption{\textit{A nice plot.}}
\end{figure}
\end{document}
Under linux, just save the file as tmp.Rnw. Then
在linux下,只需将文件保存为tmp.Rnw。然后
R CMD Sweave tmp.Rnw
pdflatex tmp.tex
#6
2
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.
还有LyX,它有一个Sweave接口。 R / LyX / Sweave接口代码位于CRAN,网址为http://cran.fhcrc.org/contrib/extra/lyx/。 LyX本身就是大多数Linux发行版。所有这些魔法都可以在Windows上运行,但它绝对不是一件容易的事。在Windows上,我推荐使用Blue Reference for R进行文化R编程的R。
#7
2
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.
好吧,我只是提醒我使用Asciidoc进行简短报告或编辑网页。现在有一个R插件(CRAN上的ascii),它允许将R代码嵌入到asciidoc文档中。语法与Markdown或Textile非常相似,因此您将非常快速地学习它。
Output are (X)HTML, Docbook, LaTeX, and of course PDF through one of the last two backends.
输出是(X)HTML,Docbook,LaTeX,当然还有PDF通过最后两个后端之一。
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.
不幸的是,我认为你不能将所有代码都包装在一个语句中。但是,它支持大量的R对象,见下文。
> methods(ascii)
[1] ascii.anova* ascii.aov* ascii.aovlist* ascii.cast_df*
[5] ascii.character* ascii.coxph* ascii.CrossTable* ascii.data.frame*
[9] ascii.default* ascii.density* ascii.describe* ascii.describe.single*
[13] ascii.factor* ascii.freqtable* ascii.ftable* ascii.glm*
[17] ascii.htest* ascii.integer* ascii.list* ascii.lm*
[21] ascii.matrix* ascii.meanscomp* ascii.numeric* ascii.packageDescription*
[25] ascii.prcomp* ascii.sessionInfo* ascii.simple.list* ascii.smooth.spline*
[29] ascii.summary.aov* ascii.summary.aovlist* ascii.summary.glm* ascii.summary.lm*
[33] ascii.summary.prcomp* ascii.summary.survfit* ascii.summary.table* ascii.survdiff*
[37] ascii.survfit* ascii.table* ascii.ts* ascii.zoo*
Non-visible functions are asterisked
#8
2
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 from base64
R package.
这是根据罗穆诺夫的回答,但仍然如此。您可以编写自己的打印,以一些HTML格式包装输出,并将输出嵌入到HTML文件中。使用数据URI方案的图片也可以这样做,例如通过使用base64 R包中的img函数。
#9
2
You can use the R2HTML
package to output a session to html and there are some similar functions in the TeachingDemos
package (see txtStart
) for output to enhanced text and word (via R2wd
). Non-graphics commands will be included in the file automatically and the current plot can be inserted by a single command.
您可以使用R2HTML包将会话输出到html,并且在TeachingDemos包中有一些类似的函数(请参阅txtStart),以输出到增强的文本和单词(通过R2wd)。非图形命令将自动包含在文件中,并且可以通过单个命令插入当前图表。
#10
1
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).
通过twitter的奇迹,有人伸出手,向我发送了一个链接到这个页面,关于一个名为“roux”的包。它创建于一年前,我从未听说过它(显然你们大多数人都没有)。
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.
我希望能够使用这个解决方案,并且看看其他R成员是否可以进入这个项目以更好地增强R.
#11
0
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
@znmeb很好地建议尝试Lyx - 一个更像LaTeX的字样前端,正如文档所指出的,本期R新闻的第2页上有一篇很好的文章与Sweave一起使用
This is how I did it in Ubuntu 10.04 follwoing the guidelines in the lyx sweave repository:
这就是我在Ubuntu 10.04中按照lyx sweave存储库中的指南进行的操作:
sudo apt-get install lyx cd ~./lyx wget http://cran.fhcrc.org/contrib/extra/lyx/preferences cd layouts wget http://cran.fhcrc.org/contrib/extra/lyx/literate* wget http://cran.fhcrc.org/contrib/extra/lyx/literate-article.layout wget http://cran.fhcrc.org/contrib/extra/lyx/literate-book.layout wget http://cran.fhcrc.org/contrib/extra/lyx/literate-report.layout wget http://cran.fhcrc.org/contrib/extra/lyx/literate-scrap.inc cd ~/texmf/tex wget http://www.biostat.jhsph.edu/~rpeng/ENAR2009/Sweave.sty
- start Lyx
- 开始Lyx
- Preferences -> Reconfigure
- 首选项 - >重新配置
- restart Lyx
- 重启Lyx
- File -> new
- 文件 - >新的
- Document -> Settings -> Document Class -> article (Sweave noweb)
- 文档 - >设置 - >文档类 - >文章(Sweave noweb)
useful links:
有用的链接:
- lyx sweave repository
- lyx sweave存储库
- R news article about Lyx and Sweave
- 关于Lyx和Sweave的R新闻文章
#1
13
As of 2012 knitr
provides a perfect solution to this problem.
截至2012年,knitr为这一问题提供了完美的解决方案。
For example, create a file with an rmd extension. Wrap your code in a couple of commands as follows:
例如,创建一个扩展名为rmd的文件。将代码包装在几个命令中,如下所示:
```{r}
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)))
```
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.
您可以通过多种方式将其转换为自包含的HTML文件。在RStudio中,您只需按一个按钮编织HTML。这是生成的HTML文件;实际查看HTML在浏览器中的显示方式,保存文件并打开它。
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.
当然,您可以并且通常会将文件分成多个R代码块。但重点是,你不必。
Here are another couple of examples I've created:
这是我创建的另外几个例子:
- Getting started with R Markdown
- R Markdown入门
- Case study in using R Markdown
- 使用R Markdown的案例研究
#2
4
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.
如果你认识LaTeX,那么sweave可能是你最好的选择。 odfWeave是一种类似的机制,但用于将代码嵌入OpenOffice.org文件中。对于HTML,有R2html包。但是所有这些都可能需要您稍微破解代码以充分利用系统。或者,您的sweave / odfweave / html模板可以在单个代码块中获取脚本的数据生成方面,并在需要的位置放置输出显示(print()语句)。您也可以在脚本中调用图形,以生成要作为单独文件嵌入文档的图形,然后将其手动包含在模板中。
For example (and this isn't a full .Rnw
file for running through sweave
) 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:
例如(并且这不是一个完整的.Rnw文件,用于通过sweave运行)你可以在模板中放置这样的东西,它可以提供R脚本的主要部分来进行分析并生成R对象:
<<run_script, eval=TRUE, echo=FALSE, results=hide>>=
source("my_script.R")
@
Then you will need to insert code chunks where you want printed output:
然后,您需要在要打印输出的位置插入代码块:
<<disp_output, eval=TRUE, echo=FALSE, results=verbatim>>=
## The results=verbatim is redundant as it is the default, as is eval=TRUE
print(summary(x)) ## etc
@
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:
使用csgillespie的示例sweave文件,我会设置这样的东西。首先是包含核心分析代码的my_script.R文件:
x <- rnorm(100)
y <- jitter(x)
corXY <- cor(x,y)
mod.lm <- lm(y~x)
Then the Sweave file
然后是Sweave文件
\documentclass[12pt]{article}
\usepackage{Sweave}
\begin{document}
An introduction
<<run_analysis, eval=TRUE,echo=FALSE, results=hide>>=
source("my_script.R")
@
% Later
Here are the results of the analysis
<<show_printed_output, echo=FALSE>>=
summary(x))
head(data.frame(x,y))
@
The correlation between \texttt{x} and \texttt{y} is:
<<print_cor, echo=FALSE>>=
corXY
@
Now a plot
\begin{figure}[h]
\centering
<<echo=FALSE, eval=TRUE, fig=TRUE, width=6, height=4>>=
plot(x,y)
@
\caption{\textit{A nice plot.}}
\end{figure}
\end{document}
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).
你似乎想要的东西不存在;将R代码和输出组合成文档文件的简单方法。那就是如果你不认为sweave和它的同类简单。您可能需要重新考虑您想要做什么或如何安排分析,图形和输出代码,但您可能最好看一下建议的选项之一(sweave,odfweave,brew,R2html)。
HTH
HTH
#3
4
I would encourage you to use Sweave, but a rudimentary functionality that is not pretty can be achieved with sink()
.
我鼓励你使用Sweave,但是使用sink()可以实现一个不太漂亮的基本功能。
A regular txt file:
常规的txt文件:
sink(file = "test.txt", type = "output")
summary(cars)
sink()
or add some HTML tags:
或者添加一些HTML标签:
sink(file = "tal_test.html", type = "output")
cat("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"", "\n")
cat("\"http://www.w3.org/TR/html4/strict.dtd\">", "\n")
cat("<HTML>", "\n")
cat("<HEAD>", "\n")
cat("<TITLE>My first HTML document</TITLE>", "\n")
cat("</HEAD>", "\n")
cat("<BODY>", "\n")
summary(cars)
cat("</BODY>", "\n")
cat("</HTML>", "\n")
sink()
#4
3
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.
大约一年前,我写了一个名为Roux的脚本。我希望能够通过运行R脚本(包括任何图像)来创建HTML脚本,而无需更改脚本。
You call Roux from the command line, like this: roux example.R
您可以从命令行调用Roux,如下所示:roux example.R
and roux will:
和roux将:
- run the script in R (requiring the Roux package first automatically)
- 在R中运行脚本(首先需要Roux包)
- syntax highlight the .Rout output using Pygments
- 语法使用Pygments突出显示.Rout输出
- insert images in the correct location
- 将图像插入正确的位置
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.
Roux R包是一个非常小的R包,它修改了plot()和一些其他函数来自动写入随机文件名而不是默认的交互式图形设备。
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.
我已经使用了很多,它对我来说非常好用,虽然我确定如果有更多的人将它用于新的包,那么会出现小问题,很可能你会有一个不同的函数来生成图形和Roux我不知道它应该为你打开一个PNG设备。
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
自从与Tal谈论此事后,我已经更新并改进了代码,现在它已经出现了:http://bitbucket.org/ananelson/roux/src
so if you run into any issues, please report them to the issue tracker there on Bitbucket.
因此,如果您遇到任何问题,请将它们报告给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.)
我已经添加了对LaTeX脚本的支持,因此您可以轻松创建包含R脚本(包括图像)的PDF的PDF。 (如果你查看example-output目录,你可以看到一个例子,找到下载它的“原始”链接。)
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.
你需要安装Python和pygments python库。如果你有旧版本的Python并遇到任何问题,请告诉我。
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.
我在博客上写过关于Roux的文章,但没有那么多宣传,因为我的工作主要集中在一个名为Dexy的新项目上,该项目旨在替代Sweave。如果您想要更多的灵活性和控制,或者对文字文档感兴趣,那么您可能也想查看Dexy。
#5
2
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.
你在问题中提到了sweave但不是真的为什么它不合适。你的问题对Sweave来说似乎很完美。实际上,您的示例代码可能来自第二个Sweave示例。
Example Sweave file
示例Sweave文件
If you know Latex then Sweave isn't that difficult. Here's your example file as a Sweave file:
如果你知道Latex,那么Sweave就不那么难了。这是你的示例文件作为Sweave文件:
\documentclass[12pt,BCOR3mm,DIV16]{scrreprt}
\usepackage{Sweave}
\begin{document}
An introduction
<<eval=TRUE,echo=TRUE>>=
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
@
Now a plot
\setkeys{Gin}{width=0.5\textwidth}
\begin{figure}[h]
\centering
<<echo=FALSE, eval=TRUE, fig=TRUE, width=6, height=4>>=
plot(x,y)
@
\caption{\textit{A nice plot.}}
\end{figure}
\end{document}
Under linux, just save the file as tmp.Rnw. Then
在linux下,只需将文件保存为tmp.Rnw。然后
R CMD Sweave tmp.Rnw
pdflatex tmp.tex
#6
2
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.
还有LyX,它有一个Sweave接口。 R / LyX / Sweave接口代码位于CRAN,网址为http://cran.fhcrc.org/contrib/extra/lyx/。 LyX本身就是大多数Linux发行版。所有这些魔法都可以在Windows上运行,但它绝对不是一件容易的事。在Windows上,我推荐使用Blue Reference for R进行文化R编程的R。
#7
2
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.
好吧,我只是提醒我使用Asciidoc进行简短报告或编辑网页。现在有一个R插件(CRAN上的ascii),它允许将R代码嵌入到asciidoc文档中。语法与Markdown或Textile非常相似,因此您将非常快速地学习它。
Output are (X)HTML, Docbook, LaTeX, and of course PDF through one of the last two backends.
输出是(X)HTML,Docbook,LaTeX,当然还有PDF通过最后两个后端之一。
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.
不幸的是,我认为你不能将所有代码都包装在一个语句中。但是,它支持大量的R对象,见下文。
> methods(ascii)
[1] ascii.anova* ascii.aov* ascii.aovlist* ascii.cast_df*
[5] ascii.character* ascii.coxph* ascii.CrossTable* ascii.data.frame*
[9] ascii.default* ascii.density* ascii.describe* ascii.describe.single*
[13] ascii.factor* ascii.freqtable* ascii.ftable* ascii.glm*
[17] ascii.htest* ascii.integer* ascii.list* ascii.lm*
[21] ascii.matrix* ascii.meanscomp* ascii.numeric* ascii.packageDescription*
[25] ascii.prcomp* ascii.sessionInfo* ascii.simple.list* ascii.smooth.spline*
[29] ascii.summary.aov* ascii.summary.aovlist* ascii.summary.glm* ascii.summary.lm*
[33] ascii.summary.prcomp* ascii.summary.survfit* ascii.summary.table* ascii.survdiff*
[37] ascii.survfit* ascii.table* ascii.ts* ascii.zoo*
Non-visible functions are asterisked
#8
2
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 from base64
R package.
这是根据罗穆诺夫的回答,但仍然如此。您可以编写自己的打印,以一些HTML格式包装输出,并将输出嵌入到HTML文件中。使用数据URI方案的图片也可以这样做,例如通过使用base64 R包中的img函数。
#9
2
You can use the R2HTML
package to output a session to html and there are some similar functions in the TeachingDemos
package (see txtStart
) for output to enhanced text and word (via R2wd
). Non-graphics commands will be included in the file automatically and the current plot can be inserted by a single command.
您可以使用R2HTML包将会话输出到html,并且在TeachingDemos包中有一些类似的函数(请参阅txtStart),以输出到增强的文本和单词(通过R2wd)。非图形命令将自动包含在文件中,并且可以通过单个命令插入当前图表。
#10
1
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).
通过twitter的奇迹,有人伸出手,向我发送了一个链接到这个页面,关于一个名为“roux”的包。它创建于一年前,我从未听说过它(显然你们大多数人都没有)。
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.
我希望能够使用这个解决方案,并且看看其他R成员是否可以进入这个项目以更好地增强R.
#11
0
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
@znmeb很好地建议尝试Lyx - 一个更像LaTeX的字样前端,正如文档所指出的,本期R新闻的第2页上有一篇很好的文章与Sweave一起使用
This is how I did it in Ubuntu 10.04 follwoing the guidelines in the lyx sweave repository:
这就是我在Ubuntu 10.04中按照lyx sweave存储库中的指南进行的操作:
sudo apt-get install lyx cd ~./lyx wget http://cran.fhcrc.org/contrib/extra/lyx/preferences cd layouts wget http://cran.fhcrc.org/contrib/extra/lyx/literate* wget http://cran.fhcrc.org/contrib/extra/lyx/literate-article.layout wget http://cran.fhcrc.org/contrib/extra/lyx/literate-book.layout wget http://cran.fhcrc.org/contrib/extra/lyx/literate-report.layout wget http://cran.fhcrc.org/contrib/extra/lyx/literate-scrap.inc cd ~/texmf/tex wget http://www.biostat.jhsph.edu/~rpeng/ENAR2009/Sweave.sty
- start Lyx
- 开始Lyx
- Preferences -> Reconfigure
- 首选项 - >重新配置
- restart Lyx
- 重启Lyx
- File -> new
- 文件 - >新的
- Document -> Settings -> Document Class -> article (Sweave noweb)
- 文档 - >设置 - >文档类 - >文章(Sweave noweb)
useful links:
有用的链接:
- lyx sweave repository
- lyx sweave存储库
- R news article about Lyx and Sweave
- 关于Lyx和Sweave的R新闻文章