9

I have a data frame which I would like to write it to a pdf file in organized fashion.

For example, my df looks like this:

Date    County    Trade
1/1/2012  USA     5
1/1/2012  Japan   4
1/2/2012  USA     10
1/3/2012  Germany 15

I would like to output to be group by Date, place a space or line break after each group;

I have this piece of code but this prints out everything to the pdf file without grouping:

library(gridExtra)
pdf("trade.pdf", height=11, width=8.5)
grid.table(df)
dev.off()

Any ideas how can best present this data set in a pdf file with grouping on Date? I like to use grid.Extra. Anybody knows how to put a title to grid.Extra?

user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 5
    The best way to do this will involve you learning some LaTeX, and using R's LaTeX tools like Sweave/knitr or the packages **xtable** or **Hmisc**. – joran Jan 14 '13 at 15:18

4 Answers4

15

This code should work:

library(gridExtra)

df <- read.table(text = 
"1/1/2012  USA     5
1/1/2012  Japan   4
1/2/2012  USA     10
1/3/2012  Germany 15"
)
names(df) <- c("Date","Country","Trade")

EqDatedf <- as.data.frame(df[1,])
EmptyLine <- data.frame(Date = "",Country = "",Trade = "")

pdf(file = "q.pdf")

for (i in 2:nrow(df)) 
{
if (as.vector(df$Date[i])  ==  as.vector(df$Date[i-1])) 
{EqDatedf <- rbind(EqDatedf, df[i,])}

else {
EqDatedf <- rbind(EqDatedf, EmptyLine)
EqDatedf <- rbind(EqDatedf, df[i,]) 
     }
}

grid.table(EqDatedf, show.rownames = FALSE)
dev.off()

enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
9

I really recommend you to use Rstudio with Knitr. It is very easy to create good reports.

For example,

\documentclass{article}
\begin{document}
<<myTable,results='asis'>>=
library(xtable)
tab <- read.table(text = 'Date    County    Trade
1/1/2012  USA     5
1/1/2012  Japan   4
1/2/2012  USA     10
1/3/2012  Germany 15',header = TRUE)
print(xtable(tab),hline.after=c(2,3))   ## print.xtable have many smart options
@
\end{document}

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • appreciate the code. I like to use grid.Extra for this project since it is fast and I need it right now. Are you familiar placing title to grid.Table? – user1471980 Jan 14 '13 at 18:14
  • @user1471980 are you asking for a solution based on the `gtable` package? I dont' understand really what is the relation with my answer. can you precise more what do you want please? – agstudy Jan 14 '13 at 18:22
  • I am trying to add a title to the output provided by grid.table. I cannot figure out how to do this? – user1471980 Jan 14 '13 at 18:37
  • You can use something like this `grid.text('My Title',y=0.8)`. – agstudy Jan 14 '13 at 18:45
  • @user1471980 use Baptiste proposition, it is better. – agstudy Jan 14 '13 at 18:46
  • @agstudy I like your solution much, but I don't know how to open LaTeX outputs. Where are they stored? My R Studio seems unable to deal with them. If I run your code, R prints the output in the Console but nothing else is produced. Do you know how I could solve this? Thank you for your help! – Riccardo Jan 22 '14 at 14:43
  • I don't get your point. Did you install Latex? Once installed you should open a new R sweave file ( using R studio) and copy the code above. – agstudy Jan 22 '14 at 16:36
2

As of 2017, there is good support in R-studio presentation formats (Markdown) with package "pander", and output to PDF via Beamer. See pander : http://rapporter.github.io/pander/#pander-an-r-pandoc-writer

Example in R-studio presentation code to print a data frame as table :

```{r}    
pander(df)
```
Dan Gustafsson
  • 316
  • 2
  • 5
1

The grid.table solution will be the quickest way to create a PDF for short tables, but this solution will not work as is if you have a table that's longer than 1 page. RStudio + knitr + longtable is probably the way to go to create nicely formatted PDFs. What you'll need is something like:

\documentclass{article}
\usepackage{longtable}
\begin{document}

<<results='asis'>>=
library(xtable)

df = data.frame(matrix(rnorm(400), nrow=100))
xt = xtable(df)
print(xt, 
      tabular.environment = "longtable",
      floating = FALSE
      )
@
\end{document}

Pls see this post for more details.

Thusi
  • 929
  • 7
  • 5