【问题标题】:Generating LaTeX tables instead of text in Rstudio在 Rstudio 中生成 LaTeX 表格而不是文本
【发布时间】:2015-05-05 09:04:21
【问题描述】:

我是乳胶新手,想生成 PDF 表格而不是文本,所以我的示例是:

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

第一组有:AAA:items:BBB:items2 和:CCC:items3。我们评估了: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)
...

最后我用了这个:

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

但是当我想生成表格而不是文本时,什么也没有发生。还有AAA、BBB、CCC。 所以我在代码的最后部分遗漏了一些东西。

【问题讨论】:

  • 您应该查看knitr 包以更轻松地集成latexr,以及查看xtable 以生成乳胶格式的表格
  • 另外,请注意您没有正确使用capture.output()。您不应将其结果分配给df。相反,只需添加一个 file 参数,其中包含您想要输出的文件的名称。

标签: r latex rstudio


【解决方案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)
```

这会将以下输出生成到 tex 文件中

% 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。

【讨论】:

  • 是否可以将此代码存储到 tex.文件,因为我已经生成了带有直方图的 pdf 报告(我使用了 'CairoPDF'、'\\begin{figure}[H]')并希望将其合并,因此该表将成为我在 15 页中的第一页中的表。跨度>
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多