Miha
Miha

Reputation: 2884

Generating LaTeX tables instead of text in Rstudio

I am new to latex, and would like to generate PDF tables instead of text so my example is:

in my METHOD.tex file I have this text:

In the first group there were: AAA: items: BBB: items2 and: CCC: items3. We assessed: EEE: implementations. Of the total: VVV: objects to the survey before replying: X111 :, rejected: X222

So AAA, BBB, CCC.... are object in which I stored numbers.

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

so now I have used this code:

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

Than I wrote:

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

And used following code (because I am generating more different reports and the numbers are changing):

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

Finnaly I used this:

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

But when I would like to generate the table instead of text nothing happens. There is still AAA, BBB, CCC. So I am missing something in last part of the code.

Upvotes: 1

Views: 1201

Answers (1)

r.bot
r.bot

Reputation: 5424

R markdown in R Studio makes this sort of thing much nicer. Blocks of R code can be declared with three backticks{r}code and then inline code with a single backtick and r.

The following does something similar to what you want.

---
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)
```

This generates the following output into a tex file

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

And automatically compiles a pdf from the tex file.

Upvotes: 1

Related Questions