2015-05-05 42 views
1

文字我是新来的乳胶,并想生成PDF表,而不是文字,让我举的例子是:生成LaTeX的表,而不是在Rstudio

在我METHOD.tex文件我有这样的文字:

在第一组有:AAA:项目:BBB:项目2和:CCC:项目3。我们评估:EEE:实现。在总数中:VVV:调查对象在回复前:X111:,被拒绝:X222

因此AAA, BBB, CCC ....是我存储数字的对象。

a1<-200 
b1<-450 
c1<-500 
d1<-1500 

所以现在我已经使用这个代码:

df<-data.frame(AAA,BBB,CCC,EEE) 

比我写道:

df <- scan("METHOD.tex", character(0), sep="\n", quiet=TRUE, encoding="UTF-8") 

并用下面的代码(因为我产生更多不同的报告和数字正在改变):

df <- gsub(pattern=":AAA:", replacement=a1, x=df) 
df <- gsub(pattern=":BBB:", replacement=b1, x=df) 
... 

Finnaly我用这个:

df<- capture.output(Hmisc::latex(df, caption="Table", rowlabel="", file="", where="H")) 

但是当我想生成表而不是文本没有任何反应。还有AAA,BBB,CCC。 所以我错过了代码的最后部分。

+2

您应该查看[knitr](http://yihui.name/knitr/)软件包,以更轻松地整合'latex'和'r',以及[xtable](http://cran.r -project.org/web/packages/xtable/)生成乳胶格式表 – scoa

+0

也请注意,您没有正确使用'capture.output()'。你不应该把它的结果赋给'df'。相反,只需添加一个'file'参数,并在其中输入想要输出的文件的名称。 – scoa

回答

1

R Studio中的R markdown使得这种事情更好。 R代码块可以用三个反引号{r}code进行声明,然后用一个反引号和r内联代码。

以下做了类似于你想要的东西。

--- 
title: "Untitled" 
author: "user" 
date: "Tuesday, May 05, 2015" 
output: 
    pdf_document: 
    keep_tex: true 
--- 

```{r} 
require(xtable) 
a1<-200 
b1<-450 
c1<-500 
d1<-1500 
``` 

In the first group there were: `r a1` items: `r b1` items2 and: `r c1` items3. We assessed: EEE: implementations. Of the total: VVV: objects to the survey before replying: X111 :, rejected: X222 

```{r, results='asis', echo = FALSE} 
data(mtcars) 
out <- xtable(head(mtcars), caption = "Head of mtcars data set", label = "mt_head") 
print(out, comment = FALSE) 
``` 

这产生如下的输出到一个特文件

% Excised preamble 
\begin{document} 
% Excised ECHO 
In the first group there were: 200 items: 450 items2 and: 500 items3. We 
assessed: EEE: implementations. Of the total: VVV: objects to the survey 
before replying: X111 :, rejected: X222 

\begin{table}[ht] 
\centering 
\begin{tabular}{rrrrrrrrrrrr} 
    \hline 
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ 
    \hline 
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4.00 \\ 
    Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4.00 \\ 
    Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1.00 \\ 
    Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1.00 \\ 
    Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2.00 \\ 
    Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1.00 \\ 
    \hline 
\end{tabular} 
\caption{Head of mtcars data set} 
\label{mt_head} 
\end{table} 

\end{document} 

而自动编译从TEX文件的PDF文件。

+0

是否可以将此代码存储到tex中。文件,因为我已经生成了带有直方图的pdf报告(我使用了'CairoPDF','\\ begin {figure} [H]'),并且希望将它合并,因此该表格将成为我的第一页中的表格。15 – Miha

+0

I解决了我的问题。谢谢。 – Miha