Search code examples
rlatexsweave

fit an xtable to a pdf file


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

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