2013-03-26 60 views
3

我正在創建一個使用xtable創建表格並放入pdf文件的sweave文檔。它可以工作,但表格不適合文檔並且缺少一些文本。有沒有辦法使文本在xtable /完全適合xtable到pdf文件中對齊?適合xtable到pdf文件

這是我的數據:

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

這是把表PDF格式的代碼:

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

回答

4

我建議您查看xtable gallery,有很多是有用的例子。基本上,如果您不想通過短接字符串來調整表格,我會看到兩個選項:

  1. 使用較小的字體。
  2. 使用橫向模式。

在這裏,我用兩者的結合:

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

請注意,您必須使用乳膠包rotating。這應該給你這樣的事情:

Output

1

另一種解決辦法是使用一些降價後端(knitrmarkdownpander包)和自動拆表在80個字符(或其他用戶指定的寬度)與pander。例如爲:

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

而結果可以被轉換爲PDF格式或乳膠容易然後用pandocpander直接從R.