2015-08-28 70 views
4

我是knitr的新手,过去我有一些非常基本的乳胶知识,所以我一直在寻找一个已经发布到某处的解决方案。但是,我无法解决我的问题。我希望有人会提供帮助。在knitr(PDF)文件中的长期使用:使用xtable(或kable)

我有一个14列和许多行的数据框,让我们说60.使用这些数据我需要在横向布局中生成PDF报告并将此数据框作为表格呈现在那里。

我发现的最接近的解决方案是在这里 tex.stackexchange.com: LaTex Longtable spanning several pages

我用了一些线索那里。但是,表格放置不正确。最右边的列在页面的右边被截断。该表格在页面末尾没有“Continued”字样。我在这里发布我的代码和图片。

我是一个解决方案,将longtables正确放置在页面上,如果我缺少任何明显的请勿拍摄:)我对此很新。

\documentclass[a4paper, landscape]{article} 
\usepackage[a4paper, margin=1in, hmarginratio=1:1, landscape]{geometry} 
\usepackage{longtable} 
\usepackage{graphicx} 
\usepackage{xcolor} 
\definecolor{myblue}{RGB}{24,57,121} 
\usepackage{lipsum} 
\usepackage{booktabs} 
\usepackage{colortbl} 
\usepackage{array} 
\usepackage{rotating} 
\usepackage{fancyhdr} 
\pagestyle{fancy} 
\fancyhead{} 
\fancyfoot{} 
\renewcommand{\headrulewidth}{0.5pt} 
\setlength\headheight{40mm} 
\begin{document} 
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}} 
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} 
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}} 
\renewcommand*{\arraystretch}{1.0} 
% 
\section{My Long Table} 
%\begin{center} 
%\begin{small} 
%\setlongtables 
%\begin{longtable} 
<<echo=FALSE, eval=TRUE, results='asis'>>= 
library(knitr) 
library(xtable) 
df <- data.frame(replicate(13, sample(1000000:9000000, 60,replace=TRUE))) 
df$Sum <- rowSums(df) 
totals <- colSums(df) 
df <- rbind(df, totals) 
names(df) <- c("Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May 2014", "Jun 2014", "Jul 2014", 
      "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014", "Jan 2015", "Sum") 
# 
dtable <- xtable(x = df) 
print (dtable 
      #, table.placement = "H" 
      , table.placement = "!htp" 
      , caption.placement = "top" 
      , include.rownames = TRUE 
      , include.colnames = TRUE 
      , size = "footnotesize" 
      , tabular.environment = 'longtable' 
      , floating = FALSE 
      #, scalebox = 0.7 
      #, width = 0.8 
      , add.to.row = list(pos = list(0),command = 
        paste("\\hline \\endfirsthead" ,       # First caption 
        "\\caption[]{My Caption should be here} \\label{tab:The Table} \\\\ \\hline", # Additional captions 
        paste("&", names(df), collapse=" "), 
        "\\\\ \\hline ", 
        "\\endhead", 
        "\\hline \\multicolumn{11}{r}{\\textit{Continued}} \\      
        \\endfoot 
        \\endlastfoot",collapse=" "))) 
@ 
%\end{longtable} 
%\end{small} 

%\end{center} 
\end{document} 

enter image description here enter image description here

回答

1

我觉得我在kableExtra的开发版本,基本上解决了这个问题。

library(knitr) 
library(kableExtra) 
kable(df, "latex", longtable = T, booktabs = T) %>% 
    kable_styling(latex_options = c("repeat_header"), font_size = 7) %>% 
    landscape() 

因为longtable不支持resizebox,你不能在latex_options使用 “scale_down” 选项。我试图将字体缩小到7,看起来很不错。

相关问题