Question

I am creating a sweave document that uses xtable to create table and puts into a pdf file. It works but table is not fitting the document and some text are missing. Is there a way to text align in xtable/fully fit an xtable to a pdf file?

This is my data:

dput(x)
structure(list(App = structure(c(3L, 2L, 1L), .Label = c("AppServer", 
"Db", "Web"), class = "factor"), Group = structure(c(2L, 1L, 
1L), .Label = c("Back", "Front"), class = "factor"), Owner = structure(c(1L, 
1L, 1L), .Label = "Infrasructure", class = "factor"), Server = structure(1:3, .Label = c("ServerA", 
"ServerB", "ServerC"), class = "factor"), NumberCPU = c(64L, 
120L, 120L), Description = structure(c(1L, 3L, 2L), .Label = c("Front End server to server web traffic", 
"Hold Web templates to generate dynamic content", "Keeps customer data and login information"
), class = "factor"), Cost = structure(1:3, .Label = c("$200,000 ", 
"$400,000 ", "$500,000 "), class = "factor")), .Names = c("App", 
"Group", "Owner", "Server", "NumberCPU", "Description", "Cost"
), class = "data.frame", row.names = c(NA, -3L))

this is the code to put the table in pdf:

print(xtable(x, caption=paste("Summary of applications"),table.placement="!h",caption.placement="top", align=c('l', 'p{1.5in}', rep('c',6) )))
Was it helpful?

Solution

I recommend checking out the xtable gallery, there are lots of examples that are useful. Basically, If you don't want to adjust your table by shorting the strings, I see two options:

  1. Use a smaller font.
  2. Use landscape mode.

Here, I use a combination of both:

\documentclass{article}
\usepackage{rotating}

\begin{document}

<<Data,echo=FALSE>>=
library(xtable)
x <- structure(list(App = structure(c(3L, 2L, 1L), .Label = c("AppServer", 
"Db", "Web"), class = "factor"), Group = structure(c(2L, 1L, 
1L), .Label = c("Back", "Front"), class = "factor"), Owner = structure(c(1L, 
1L, 1L), .Label = "Infrasructure", class = "factor"), Server = structure(1:3, .Label = c("ServerA", 
"ServerB", "ServerC"), class = "factor"), NumberCPU = c(64L, 
120L, 120L), Description = structure(c(1L, 3L, 2L), .Label = c("Front End server to server web traffic", 
"Hold Web templates to generate dynamic content", "Keeps customer data and login information"
), class = "factor"), Cost = structure(1:3, .Label = c("$200,000 ", 
"$400,000 ", "$500,000 "), class = "factor")), .Names = c("App", 
"Group", "Owner", "Server", "NumberCPU", "Description", "Cost"
), class = "data.frame", row.names = c(NA, -3L))
@

<<tab,echo=FALSE,results='asis'>>=
print(xtable(x, caption=paste("Summary of applications"),
             caption.placement="top",
             align=c('l', 'p{1.5in}', rep('c',6))),
             size="footnotesize",
             floating.environment="sidewaystable")
@


\end{document}

Note that you have to use the LaTex package rotating. This should give you something like this:

Output

OTHER TIPS

An alternative solution would be to use some markdown backend (knitr, markdown or pander packages) and split the table automatically to 80 character (or other user specified width) with pander. E.g.:

> library(pander)
> res <- structure(list(App = structure(c(3L, 2L, 1L), .Label = c("AppServer", "Db", "Web"), class = "factor"), Group = structure(c(2L, 1L, 1L), .Label = c("Back", "Front"), class = "factor"), Owner = structure(c(1L, 1L, 1L), .Label = "Infrasructure", class = "factor"), Server = structure(1:3, .Label = c("ServerA", "ServerB", "ServerC"), class = "factor"), NumberCPU = c(64L, 120L, 120L), Description = structure(c(1L, 3L, 2L), .Label = c("Front End server to server web traffic", "Hold Web templates to generate dynamic content", "Keeps customer data and login information"), class = "factor"), Cost = structure(1:3, .Label = c("$200,000 ", "$400,000 ", "$500,000 "), class = "factor")), .Names = c("App", "Group", "Owner", "Server", "NumberCPU", "Description", "Cost"), class = "data.frame", row.names = c(NA, -3L))
> pander(res)

----------------------------------------------------
   App     Group      Owner      Server   NumberCPU 
--------- ------- ------------- -------- -----------
   Web     Front  Infrasructure ServerA      64     

   Db      Back   Infrasructure ServerB      120    

AppServer  Back   Infrasructure ServerC      120    
----------------------------------------------------

Table: Table continues below


---------------------------------------
     Description             Cost  
------------------------------ --------
Front End server to server web $200,000
    traffic                        

Keeps customer data and login  $400,000
       information                     

Hold Web templates to generate $500,000
       dynamic content                 
---------------------------------------

And the result can be converted to pdf or LaTeX easily then with pandoc or with pander directly from R.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top