R 程式設計/出版質量輸出
您可以使用 format() 函式來控制顯示物件的位數和其他特性。
> df <- data.frame(x = rnorm(10), y = rnorm(10))
> print(df)
x y
1 -0.4350953 -0.6426477
2 -0.5947293 -0.2389625
3 -0.7061850 -2.4382016
4 -0.3384038 -0.6322842
5 0.2713353 0.5396409
6 -1.1144711 -2.0321274
7 -1.0356184 1.7217443
8 -2.6665278 -0.3621377
9 0.2975570 0.1598905
10 1.4631458 -0.7995652
> print(format(df, digits=3, scientific=T))
x y
1 -4.35e-01 -6.43e-01
2 -5.95e-01 -2.39e-01
3 -7.06e-01 -2.44e+00
4 -3.38e-01 -6.32e-01
5 2.71e-01 5.40e-01
6 -1.11e+00 -2.03e+00
7 -1.04e+00 1.72e+00
8 -2.67e+00 -3.62e-01
9 2.98e-01 1.60e-01
10 1.46e+00 -8.00e-01
Sweave[1] 是一種文學程式設計語言,它集成了 LaTeX 和 R 程式碼。Sweave 檔案生成一個 LaTeX 檔案和一個 R 檔案,這些檔案可以編譯。Roger Koenker[2]、Meredith 和 Racine (2009)[3] 以及 Charles Geyer[4] 認為 Sweave 有利於可重複的計量經濟學/統計學研究。
Sweave 有一些替代方案,用於進行文學程式設計。其中一個是 Babel,它包含在 Emacs Orgmode[5] 中。此工具允許匯出到 LaTeX 和 HTML。還可以包括用於各種程式語言(R、Ruby 等)的程式碼塊。
主要思想是編寫一個包含 LaTeX 和 R 程式碼的檔案。LaTeX 程式碼以以下開頭@而 R 程式碼以以下開頭<<>>=(一些選項可以包含在以下之間<<和>>).
@
% Some LaTeX code
\section{Results}
I show that ...
<<>>=
# Some R code
qnorm(.975)
@
% Some LaTeX code
$$
\Phi^{-1}(.975) = 1.96
$$
該檔案以副檔名儲存.Rnw或.rnw。最後,使用以下命令從該檔案中提取一個 R 檔案Stangle()以及一個使用以下命令生成的 LaTeX 檔案Sweave()。以下是一個名為file.Rnw的檔案示例,它生成file.tex和file.R
> Sweave("file.Rnw")
Writing to file file.tex
Processing code chunks ...
1 : echo keep.source term verbatim pdf
2 : echo keep.source term verbatim pdf
> Stangle("file.Rnw")
Writing to file file.R
然後,您可以對 file.tex 執行 LaTeX。這可以使用以下方法完成system()函式或texi2dvi().
# Example under Windows :
system("pdflatex.exe -shell-escape file.tex") # runs pdflatex
system("open file.pdf") # opens the pdf
請注意,您可能需要從網際網路上下載 Sweave.sty,因為它不是標準 MikTeX 發行版的一部分。
您還可以使用以下命令在文字中新增結果\Sexpr{}函式。
$
\Phi^{-1}(.975) = \Sexpr{qnorm(.975)}
$
有一些選項。這些選項可以包含在每個程式碼塊中,也可以包含在 Sweave 命令中。
- 對於圖形,您可以使用以下命令將它們包含在 tex 檔案中fig=T或者使用以下命令不包含它們fig=F.
預設情況下,圖形以 pdf 和 eps 檔案格式匯出。如果您只想使用一種格式,請使用以下命令禁止另一種格式pdf=F或eps=F選項。
- 可以使用以下命令在 tex 檔案中顯示 R 程式碼echo=T。如果您不想將它包含在 tex 檔案中,請使用echo=F.
- 可以使用以下命令評估 R 程式碼eval=T。如果您不想評估 R 程式碼,請使用eval=F.
- 結果
- results=tex將輸出視為 LaTeX 程式碼
- results=verbatim將輸出視為 Verbatim(預設值)
- results=hide在 LaTeX 輸出中不包含結果
這些選項可以傳遞給Sweave()函式。
Sweave("file.Rnw", pdf = T, eps=F, echo = F, results = "verbatim")
它們也可以傳遞給每個程式碼塊。
<<fig=T,pdf=T,eps=F>>=
plot(rnorm(100), col = "red")
@
Sweave 的主要問題是,很少有文字編輯器包含對 Sweave 的語法突出顯示功能。以下是一些例外
- RStudio 是一款非常好的解決方案。它易於安裝和使用,並且包含用於執行 Sweave 檔案的按鈕。
- Vim 為 Sweave 檔案提供了語法突出顯示功能(R no web syntax)。
- Emacs + ESS (Emacs Speaks Statistics) 為 Sweave 檔案提供全面支援。它包括一個用於執行 Sweave 檔案的鍵盤快捷鍵,以及在 LaTeX 和 R 之間切換的語法突出顯示功能。
- Eclipse StatET 外掛為 Sweave(LaTeX/R)文件提供了支援,其中包含所有基本功能(語法突出顯示、括號匹配、切換註釋等),以及對 R 程式碼塊的檢測。
以下是一些 Sweave 文件示例
- Charles Geyer foo.Rnw 示例
- Julien Barnier 的 R 入門(法語文件)
- 技巧 : 鍵入filetype:Rnw或filetype:Snw在 Google 中搜索以獲取 Sweave 檔案
- 請注意,您可以透過瀏覽 R 庫資料夾來找到大量示例。文件通常使用 Sweave 編寫,並且 Sweave 檔案通常包含在包中。例如,在np 包的 doc 資料夾中。
一些講義
- "使用 Sweave 和 DOCSTRIP 進行文學程式設計"(pdf)作者:Michael Lundholm
- Charles Geyer 2008 "Sweave 演示"(pdf)(簡短)
- 使用 APA 樣式學習 Sweave[6]
一些包
- pgfSweave 包
- ascii 包
- cacheSweave
- exam 用於自動生成考試
一些替代的文學程式設計包
- odfWeave 包用於使用 OpenOffice 進行 Sweave。
- knitr 包
- decumar,Hadley Wickham[7] 為 R 提供的文學程式設計介面
- relax 包
- wikirobot[8] 與 Sweave 相似,但適用於 MediaWiki。
Pubprint 是一款小型實用程式,能夠將統計檢驗的輸出轉換為可供出版的輸出。Pubprint 能夠將輸出匯出為多種格式(HTML、LaTeX、Markdown 和純文字),但不幸的是,它僅支援 APA 樣式(美國心理學會的出版樣式)。但是,這種樣式被廣泛使用,並且可能在更多情況下適用。
> library("pubprint")
> pprint(t.test(rnorm(30), rnorm(30)))
[1] "(\\ensuremath{M\\ifmmode_{x}\\else\\textsubscript{x}\\fi=-0.05,M\\ifmmode_{y}\\else\\textsubscript{y}\\fi=0.09,t[57.74]=-0.49,p=.628})"
顯然,pubprint 列印的是 LaTeX 格式化的字串,但可以更改輸出格式(根據手冊,pubprint 旨在與 knitr 一起使用,並且如果與 knitr 一起使用,它會自動檢測輸出格式)。
> pp_opts_out$set(pp_init_out("plain"))
> pprint(t.test(rnorm(30), rnorm(30)))
[1] "(M_x=-0.14,M_y=-0.24,t[57.4]=0.41,p=.682)"
> pprint(cor.test(rnorm(30), rnorm(30)))
[1] "(r=-.08,p=.693)"
輸出可以貼上到文件中,也可以包含在 knitr/sweave\Sexpr{}語句中。
R 包含許多函式,這些函式允許它將結果匯出到 LaTeX[9]。
toLatex()位於utils 包中。
- 請注意toLatex()不處理矩陣。
- toLatex()已在memisc 包中進行了調整,以處理矩陣和 ftable。
> toLatex(sessionInfo())
\begin{itemize}
\item R version 2.2.0, 2005-10-06, \verb|powerpc-apple-darwin7.9.0|
\item Base packages: base, datasets, grDevices,
graphics, methods, stats, utils
\end{itemize}
- mat2tex()(sfsmisc)將矩陣匯出到 LaTeX。
- tex.table()(cwhmisc)包將資料框匯出到 LaTeX 表格。
> tex.table(mydat)
\begin{table}[ht]
\begin{center}
\begin{footnotesize}
\begin{tabular}{r|rrr}
\hline
& y & x1 & x2\\ \hline
1 & -0.09 & -0.37 & -1.04\\
2 & 0.31 & 0.19 & -0.09\\
3 & 3.78 & 0.58 & 0.62\\
4 & 2.09 & 1.40 & -0.95\\
5 & -0.18 & -0.73 & -0.54\\
6 & 3.16 & 1.30 & 0.58\\
7 & 2.78 & 0.34 & 0.77\\
8 & 2.59 & 1.04 & 0.46\\
9 & -1.96 & 0.92 & -0.89\\
10 & 0.91 & 0.72 & -1.1\\
\hline
\end{tabular}
\end{footnotesize}
\end{center}
\end{table}
- xtable()(xtable)將各種物件(包括表格、資料框、lm、aov 和 anova)匯出到 LaTeX。
> # lm example
> library(xtable)
> x <- rnorm(100)
> y <- 2*x + rnorm(100)
> lin <- lm(y~x)
> xtable(lin)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sun Sep 23 21:54:04 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & -0.0407 & 0.0984 & -0.41 & 0.6803 \\
x & 2.0466 & 0.1043 & 19.63 & 0.0000 \\
\hline
\end{tabular}
\end{center}
\end{table}
> # table example
> x <- sample(1:10, 30, replace = T)
> tab <- table(x)
> tab <- cbind(tab, prop.table(tab))
> colnames(tab) <- c("N.", "Prop.")
> xtable(tab, digits = c(0, 0, 2))
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sun Sep 23 22:06:36 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& N. & Prop. \\
\hline
1 & 5 & 0.17 \\
3 & 1 & 0.03 \\
4 & 3 & 0.10 \\
5 & 6 & 0.20 \\
6 & 5 & 0.17 \\
7 & 3 & 0.10 \\
8 & 2 & 0.07 \\
9 & 2 & 0.07 \\
10 & 3 & 0.10 \\
\hline
\end{tabular}
\end{center}
\end{table}
另請參閱
- Romain François 的highlight 包將 R 程式碼匯出到 LaTeX 和 HTML。
- format.df()和latex()位於Hmisc 包中。
- MEMISC 和quantreg 包包含其他latex()函式。
- estout 包。
- reporttools 包包含一些用於描述性統計表的功能[10]。
- stargazer 包提供了一種簡單的方法來將回歸結果匯出到 LaTeX[11]
- texreg 提供了類似的功能[12]。
- estout 包提供類似於 Stata 的功能esttab和estout實用程式[13]。估計結果使用eststo()儲存,並使用esttab()列印。它們可以匯出到 CSV 和 LaTeX。這些功能支援lm, glm和plm物件(參見 plm 包)。
- apsrtable()(apsrtable)以類似於美國政治科學評論出版標準的方式將多個迴歸的結果匯出到 LaTeX。
- Thextable(xtable 包)匯出資料框、矩陣、估計結果[14]。xtable()也可以用來將結果匯出到 HTML 檔案。
- The outreg() function[15] 由 Paul Johnson 開發,類似於 Stataoutreg[16] 函式。請參閱關於此主題的 "R you ready ?" 部落格文章。
- mtable()和toLatex()在 'memisc 包中。
N <- 10^3
u <- rnorm(N)
x1 <- rnorm(N)
x2 <- x1 + rnorm(N)
y <- 1 + x1 + x2 + u
lm1 <- lm(y ~ x1 + x2 )
lm2 <- lm(y ~ x1 + x2 + I(x1*x2))
library(estout)
estclear() # clear all the eststo objects
eststo(lm1)
eststo(lm2)
esttab() # print it
library("apsrtable")
apsrtable(lm1,lm2)
library(xtable)
xtable(lm1)
tab <- xtable(lm1)
print(tab,type="html")
source("http://pj.freefaculty.org/R/WorkingExamples/outreg-worked.R")
outreg(list(lm1,lm2))
library("memisc")
toLatex(mtable(lm1,lm2))
rpublisher[17] 是一種將結果釋出到 HTML 的 literate programming 語言(它基於 python,最後一次更新是在 2008 年)。
參見 R2HTML、xtable、hwriter、prettyR、highlight、HTMLUtils
wiki.table()在 hacks 包中將矩陣或資料框匯出到 Mediawiki 表標記(如在本維基和其他許多維基中使用的那樣)。
> wiki.table(matrix(1:16,4),caption="Test")
{|
|+ Test
| 1 || 5 || 9 || 13
|-
| 2 || 6 || 10 || 14
|-
| 3 || 7 || 11 || 15
|-
| 4 || 8 || 12 || 16
|}
- ↑ The Sweave Homepage http://www.stat.uni-muenchen.de/~leisch/Sweave/
- ↑ http://www.econ.uiuc.edu/~roger/repro.html
- ↑ Meredith, E. and J.S. Racine (2009), “Towards Reproducible Econometric Research: The Sweave Framework,” Journal of Applied Econometrics, Volume 24, pp 366-374.
- ↑ Charles Geyer "Why Reproducible Research is the Right Thing" http://www.stat.umn.edu/~charlie/Sweave/
- ↑ Babel in Emacs Orgmode http://orgmode.org/worg/org-contrib/babel/intro.html
- ↑ Ista ZahnLearning To Sweave in APA Style, The PracTeX Journal 2008, 1
- ↑ decumar git archive : http://github.com/hadley/decumar
- ↑ wikirobot http://r-forge.r-project.org/projects/wikirobot/
- ↑ 如果你想了解 LaTeX,請參閱 LaTeX Wikibook
- ↑ reporttools: R Functions to Generate LaTeX Tables of Descriptive Statistics
- ↑ http://www.r-statistics.com/2013/01/stargazer-package-for-beautiful-latex-tables-from-r-statistical-models-output/
- ↑ http://www.r-bloggers.com/texreg-a-package-for-beautiful-and-easily-customizable-latex-regression-tables-from-r/
- ↑ estout : http://repec.org/bocode/e/estout/
- ↑ xtable on dataninja blog
- ↑ The outreg() function http://pj.freefaculty.org/R/WorkingExamples/outreg-worked.R
- ↑ Stata outreg http://ideas.repec.org/c/boc/bocode/s375201.html
- ↑ rpublisher : http://code.google.com/p/rpublisher/
